How To Convert UITextField value Into NSNumber? [duplicate] - ios

This question already has answers here:
How to convert an NSString into an NSNumber
(18 answers)
Closed 8 years ago.
I've tried to convert the UITextfield Value Into a float first then convert it to an NSNumber but that doesn't seem to work as seen below.
float carb = [self.carbGrams.text floatValue];
NSLog(#"%.2f", carb);
nCarbGrams = carb;
In the last line I get this error message:
Assigning to 'NSNumber *__strong' from incompatible type 'float'
I've then just tried assigning carb as an NSNumber by doing this
NSNumber *carb = [self.carbGrams.text floatValue];
NSLog(#"%.2f", carb);
nCarbGrams = carb;
But I get this error message instead :
Initializing 'NSNumber *__strong' with an expression of incompatible type 'float'
As I've read I though NSNumber could accept any type of numeric value but I seem to be incorrect, can someone please evaluate the problem?

You can use Objective-C literal syntax:
NSNumber *carb = #([self.carbGrams.text floatValue]);

You need to create an object of typ NSNumber from the float. As such
float carb = [self.carbGrams.text floatValue];
NSNumber *nCarb = [NSNumber numberWithFloat: carb];
or why not use the fancy (new) literal syntax
NSNumber *nCarb = #([self.carbGrams.text floatValue]);

Try this:
NSNumber *carb = [NSNumber numberWithFloat:[self.carbGrams.text floatValue]];

Related

NSDictionary with float value return wrong conversion

I have NSDictionary with floating values, I am trying to get values like this
[[NSDictionary valueForKey:#"some_value"] floatValue];
but looks like value not rounded correctly.
For example:
I have value in dictionary: #"0.35" but after above conversion returns 0.34999999403953552 float value, #"0.15" converts into 0.15000000596046448 etc.
When you are converting value from string to float, it lost its precision so you have to format it again. Please look at below the question you will get the better idea of that.
Make a float only show two decimal places
Converting Dictionary value to float
NSString *str = [dict objectForKey:#"key"];
CGFloat strFloat = (CGFloat)[str floatValue];
Try this code, It will work for you. Tested!
float myFloatValue= 2345.678990;
NSString* formattedNumber = [NSString stringWithFormat:#"%.02f", myFloatValue];
NSLog(#"Changed Float Value:%#",formattedNumber);

Getting a warning in XCode about an NSMutableDictionary key that should be NSNumber

So I have the following in an Xcode 6.3 project where I have _menuHeaderPositions which should hold a keys of a menuHeaderID and an NSNumber which represents the contentOffset:
// _menuHeaderPositions is a NSMutableDictionary
// should be a NSNumber created from an NSUInteger
[_menuHeaderPositions setObject:[NSNumber numberWithFloat:_runningYPosition] forKey:[NSNumber numberWithInteger:menuHeader.menuHeaderID]];
// so unsigned long because of complaints about int
firstButton.tag=(unsigned long)menuHeaderID;
... later firstButton -> thisTap.view
// Works Fine
NSLog(#"you tapped me %lu", [_menuHeaderPositions objectForKey:[NSNumber numberWithInteger: thisTap.view.tag]]);
// THIS IS THE ISSUE
// Implicit Conversion loses integer precision: 'NSInteger' (aka long) to 'int'
NSNumber *pos=[_menuHeaderPositions objectForKey:[NSNumber numberWithInt:thisTap.view.tag]];
But I get this error and am pretty clueless as to what is going on here:
Implicit Conversion loses integer precision: 'NSInteger' (aka long) to 'int'
I can access the correct value later as [pos floatValue] but how do I get this warning to go away?
edit 1
trying this didn't seem to work:
NSNumber *pos=[_menuHeaderPositions objectForKey:[NSNumber numberWithInt:#(thisTap.view.tag)]];
The tag property has a type of NSInteger. You are attempting to pass this NSInteger to a method (numberWithInt:) that expects an int.
You have two choices:
Use NSNumber numberWithInteger:
NSNumber *pos=[_menuHeaderPositions objectForKey:[NSNumber numberWithInteger:thisTap.view.tag]];
Use modern boxing: #(thisTap.view.tag)
NSNumber *pos=[_menuHeaderPositions objectForKey:#(thisTap.view.tag)];
You can also use modern dictionary syntax and the line simply becomes:
NSNumber *pos = _menuHeaderPositions[#(thisTap.view.tag)];
First you should clear the data type of your menuHeader.menuHeaderID, which has two identity —— the key of the dictionary and the tag of the view.
As #rmaddy said, the tag property of the UIButton has type of NSInteger, which is NOT simply int.
This is the doc of numberWithInt: and numberWithInteger:.
Briefly speaking, use numberWithInteger: rather than numberWithInt:.
firstButton.tag = (NSInteger)menuHeaderID;
NSNumber *pos = [_menuHeaderPositions objectForKey:[NSNumber numberWithInteger:thisTap.view.tag]];

iOS NSNumber Invalid operands to binary expression (NSNumber *" and 'double')

I have the following line of code
NSNumber *myValue = loadTempValue*0.420;
where I am trying to set the value of *myValue to the value of loadTempValue*0.420,
However, I get the error
Invalid operands to binary expression ('NSNumber *" and 'double')
Can someone advise how to set this out?
It seems that loadTempValue is also an NSNumber. In that case you want:
NSNumber *myValue = #([loadTempValue doubleValue] * 0.420);
Why are you using NSNumber objects for these values?
If loadTempValue was a double you could just do:
double myValue = loadTempValue * 0.42;
loadTempValue is an NSNumber * and 0.420 is a float. You're trying to multiply an object by a float which the compiler does not understand.
What you want to do it get the float value from loadTempValue and then multiply that by 0.420. You do that this way:
[loadTempValue floatValue] * 0.420;
From there, it seems like you want to put that value back into an NSNumber * object, you do that like this:
#([loadTempValue floatValue] * 0.420);
The #( ... ) syntax was recently introduced to Objective-C. It's called object literal notation for numbers. It is a shorthand way of writing [NSNumber numberWithFloat: ...]
Finally, you will want to assign the result to a variable called myValue; you can accomplish that like this:
NSNumber *myValue = #([loadTempValue floatValue] * 0.420);
Try:
NSNumber *myValue = #([loadTempValue doubleValue] * 0.420);
or
NSNumber *myValue = [NSNumber numberWithDouble:([loadTempValue doubleValue] * 0.420)];

Objective C: NSNumber from Array to float [duplicate]

This question already has an answer here:
How to convert NSNumber objects for computational purposes?
(1 answer)
Closed 9 years ago.
I have an array:
SomeArray =[[NSMutableArray alloc]initWithObjects:[NSNumber numberWithFloat:50.0],[NSNumber numberWithFloat:120.0],[NSNumber numberWithFloat:200.0],nil];
When I retrieve it:
NSNumber* Target = [SomeArray objectAtIndex:0];
When I NSLog it:
NSLog(#"Target %d",Target);
it return something funky like
2013-06-26 01:47:58.940 KKK[1027:c07] Target 121016880
What is the proper way to do this?? I just need the number in the array to be used as float.
You are retrieving a NSNumber, which is an object.
%d is for logging decimals, which is not your case.
Either you log it with
NSLog(#"Target %#", target);
or you convert it to a float and use %f
NSLog(#"Target %f", [target floatValue]);
And PLEASE don't use capitalized identifiers for variables!
You are asking for a decimal %d in the log and also a primitive type. NSNumber is an object that wraps a primitive type numbers. So you can do like that
NSLog(#"target %f",[Target floatValue]) or NSLog(#"target %#",Target). With the first you are sending a message to the object to unwrap the float value with the latter you are asking for the object description that in this case is the number

Converting float to NSNumber

I am missing something basic, here. Must have forgotten it. But basically, I have the following code the purpose take an NSNumber, convert it to float, multiply it by 2 and return the result to an NSNumber. I get an error on the last step, and am stumped. What should I do there.
NSNumber *testNSNumber = [[[NSNumber alloc] initWithFloat:200.0f] autorelease];
float myfloatvalue = [testNSNumber floatValue] * -2;
NSLog(#" Test float value %1.2f \n\n",myfloatvalue);
[testNSNumber floatValue:myfloatvalue]; // error here is floatValue is not found
The method floatValue of NSNumber does not take parameters. If you would like to set a new float number, you need to re-assign testNSNumber, because NSNumber does not have a mutable counterpart:
testNSNumber = #(myfloatvalue); // The new syntax
or
testNSNumber = [NSNumber numberWithFloat: myfloatvalue]; // The old syntax
Swift 3.0:
let testNSNumber: NSNumber = NSNumber(value: myfloatvalue)
Swift 2.0 version:
let testNSNumber: NSNumber = NSNumber(float: myfloatvalue)

Resources