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

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)];

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);

XCode not using decimal number, only using whole digit

I am trying to multiply three TextField's values which are in numbers. These values can contains decimal places. In multiplication Objective-C does not consider decimal places. For example, if the number was 3.45, it would only multiply by 3 and not with the decimals as well. Might be a basic question but i am stuck and really need help!
Here's the code i'm using so far:
- (IBAction)CalculateSimple:(id)sender {
int sum = [[principal text] intValue] * [[rate text] intValue] * [[loan text] intValue];;
total.text = [NSString stringWithFormat:#"$%d", sum]; }
Use double instead of int:
double sum = [[principal text] doubleValue] *
[[rate text] doubleValue] *
[[loan text] doubleValue];
total.text = [NSString stringWithFormat:#"$%f", sum];
You are using intValue (which returns integer type, thus your calculations are done on integers). You need to use floatValue or doubleValue (depending on your needs).
Also - check int/float/double types, if you don't know them yet.
You transform each of your operands to an intValue. Furthermore your result is also declared as an int variable.
Variables of type int can only store whole numbers.
If you want to work with floating point numbers use an appropriate data type like float or double depending on your desired precision and the size of the value.
Take the documentation on values in Objective-C as a reference.
When you are printing your result you also have to match the placeholder to the data type.
See the String Programming Guide
So with that in mind your method would look like this:
- (IBAction)CalculateSimple:(id)sender {
float sum = [[principal text] floatValue] * [[rate text] floatValue] * [[loan text] floatValue];
total.text = [NSString stringWithFormat:#"$%f", sum];
}

How To Convert UITextField value Into NSNumber? [duplicate]

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]];

NSNumber to float Value

When I convert NSNumber to float value using 'floatValue', there is a difference in precision. Example, I have a NSNumber 'myNumber' having value 2.3, and if I convert myNumber to float using 'floatValue', its value becomes, 2.29999. But I need exactly 2.30000. There is no problem with number of zeros after 2.3, I need '2.3' instead of '2.9'.
How can I do so?
I had similar situation where I was reading value and assigning it back to float variable again.
My Problem statement:
NSString *value = #"553637.90";
NSNumber *num = #([value floatValue]); // 1. This is the problem. num is set to 553637.875000
NSNumberFormatter *decimalStyleFormatter = [[NSNumberFormatter alloc] init];
[decimalStyleFormatter setMaximumFractionDigits:2];
NSString *resultString = [decimalStyleFormatter stringFromNumber:num]; // 2. string is assigned with rounded value like 553637.88
float originalValue = [resultString floatValue]; // 3. Hence, originalValue turns out to be 553637.88 which wrong.
Following worked for me after changing lines:
NSNumber *num = #([value doubleValue]); // 4. doubleValue preserves value 553637.9
double originalvalue = [resultString doubleValue]; // 5. While reading back, assign to variable of type double, in this case 'originalValue'
I hope this would be helpful. :)
If you need exact precision, don't use float. Use a double if you need better precision. That still won't be exact. You could multiply myNumber by 10, convert to an unsigned int and perform your arithmetic on it, convert back to a float or double and divide by 10 and the end result might be more precise. If none of these are sufficiently precise, you might want to look into an arbitrary precision arithmetic library such as GNU MP Bignum.
I've done the following but it is showing me correctly
NSNumber *num = [NSNumber numberWithFloat:2.3];
float f = [num floatValue];
NSLog(#"%f", f);
You can play with something like this:
float x = 2.3f;
NSNumber *n = [NSNumber numberWithFloat:x];
NSNumberFormatter *fmt = [[NSNumberFormatter alloc] init];
[fmt setPositiveFormat:#"0.#"];
NSString *s = [fmt stringFromNumber:n];
float f = [s floatValue];

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