Reputation: 111
I am assigning a float value to a UIlabel and when the limit exceeds its limit to handle, the value changes and shows me incorrect result.I am using autoresize text with a minimum font text value and truncate tail. I am making a calculator.If i want a result for 50000x50000 the the result should be 2500000000.0000 but the label shows something like 2499999548.0000
Upvotes: 3
Views: 329
Reputation: 33389
If you have a read of http://en.wikipedia.org/wiki/Floating_point... you will find that any number larger than about 10,000 needs to be using a 64 bit float instead of a 32 bit float if you want to have four decimal places.
The double
data type is a 64 bit float.
Be especially careful of CGFloat
because that will be 64 bit on an iPhone 5S and 32 bit on an iPhone 5C and any older iPhone.
A 64 bit float is accurate for numbers in the billions. Anything larger than that and you'll want to start working with the NSDecimalNumber
class. This is probably what you should do for a calculator app.
Here's a demonstration of why you shouldn't use float or double when you need accuracy:
float aFloat = 10000.2;
float bFloat = 10000.0;
NSLog(@"%.15f", aFloat - bFloat); // 0.200195312500000
double aDouble = 10000.2;
double bDouble = 10000.0;
NSLog(@"%.15f", aDouble - bDouble); // 0.200000000000728
NSDecimalNumber *a = [NSDecimalNumber decimalNumberWithString:@"10000.2"];
NSDecimalNumber *b = [NSDecimalNumber decimalNumberWithString:@"10000.0"];
NSLog(@"%@", [a decimalNumberBySubtracting:b]); // 0.2
It's even worse with really big numbers:
float aFloat = 10000000000000000000000.2;
float bFloat = 10000000000000000000000.0;
NSLog(@"%.15f", aFloat - bFloat); // 0.000000000000000
double aDouble = 10000000000000000000000.2;
double bDouble = 10000000000000000000000.0;
NSLog(@"%.15f", aDouble - bDouble); // 0.000000000000000
NSDecimalNumber *a = [NSDecimalNumber decimalNumberWithString:@"10000000000000000000000.2"];
NSDecimalNumber *b = [NSDecimalNumber decimalNumberWithString:@"10000000000000000000000.0"];
NSLog(@"%@", [a decimalNumberBySubtracting:b]); // 0.2
Upvotes: 2
Reputation: 8066
Adding 'f' to the end of the value should do the trick. Unlike 5.0 , 5.0f gives you the literal value of a float.
Try 50000.0f x 50000.0f
Upvotes: 0
Reputation: 508
float num;
num = 50000.0f * 50000.0f;
Lbl.text = [NSString stringWithFormat:@"%f",num];
Its work for me perfectly...
Upvotes: 0
Reputation: 47119
If you want to display your calculation then use (Here You need to cast float.)
float mycalculation = (float)50000*50000;
yourLableName.text = [NSString stringWithFormat:@"%f", mycalculation ];
I use above code it working properly for me.
Upvotes: 0
Reputation: 1805
Type define your result as well and preferably in such cases use double than just float
These two lines on code will work..
double num = (double)50000*50000;
label.text = [NSString stringWithFormat:@"%.1f",num];
Upvotes: 0
Reputation: 122
Try This, Its working .....
Ask me if any query...
NSString *a = @"50000";
NSString *b = @"50000";
float ValueA = [a floatValue]; //instead of "a" you can pass Textfield1.text
float ValueB = [b floatValue]; //instead of "b" you can pass Textfield2.text
NSLog(@"%@",[self getStringFromFloat:ValueA*ValueB]);
-(NSString *)getStringFromFloat:(float)yourFloatValue
{
return [NSString stringWithFormat:@"Value =%.4f",yourFloatValue];
}
Upvotes: 0