UIlabel gives an invalid float value at runtime

I assign a float value to UIlabel, and when the limit exceeds its limit for processing, the value changes and shows me the wrong result. I use auto-cut text with a minimal font text value and truncation of the tail. I am making a calculator. If I need a result for 50000x50000, the result should be 2500000000.0000, but something like 2499999548.0000 is displayed on the label

+3
source share
6 answers

If you read http://en.wikipedia.org/wiki/Floating_point ... you will find that any number greater than about 10,000 should use 64 bit float instead of 32 bit float if you want to have four decimal places .

The data type doubleis a 64-bit float.

Be especially careful with CGFloat, because it will be 64 bits on the iPhone 5S and 32 bits on the iPhone 5C and any previous iPhone.

64-bit float is accurate for billions of numbers. Anything more and you will want to get started with the class NSDecimalNumber. This is probably what you should do for the calculator application.

https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/classes/nsdecimalnumber_class/reference/reference.html

, float double, :

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

:

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
+2

, ( float.)

float mycalculation = (float)50000*50000;
yourLableName.text = [NSString stringWithFormat:@"%f", mycalculation ];

, .

0

Try it, its work .....

Ask me if any request ...

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];
}
0
source
float num;
num = 50000.0f * 50000.0f;
Lbl.text = [NSString stringWithFormat:@"%f",num];

His work is perfect for me ...

0
source

Enter your result as well and, preferably, in such cases use double, than just float

These two lines of code will work.

 double num = (double)50000*50000;
 label.text = [NSString stringWithFormat:@"%.1f",num];
0
source

Adding 'f' to the end of the value should do the trick. Unlike 5.0, 5.0f gives you the literal float value.

Try 50000.0f x 50000.0f

0
source

All Articles