How display the number of decimal places is uncertain float NSNumber to UILabel as in json?

59 Views Asked by At

I have an NSNumber object from json.
I want to convert to NSString for display, but the result is weird.

    NSNumber *num = @(98.9);
    NSString *numString = [num stringValue];// output 98.90000000000001

The JSON I got from the server is

{"Price": 98.9}

I need to show it to the UILabel 98.9, But the result is 98.90000000000001
The number of decimal places is uncertain
Maybe

{"Price": 98.91}

or

{"Price": 98.901}

I need to display the same as in json

1

There are 1 best solutions below

1
lazyTank On

You can do it with stringWithFormat:

NSNumber *num = @(98.9);
NSString * numStr = [NSString stringWithFormat:@"%.4lf",[num floatValue]];