0

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.

Mukesh
  • 777
  • 7
  • 20
ArisRS
  • 1,362
  • 2
  • 19
  • 41

3 Answers3

0

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

Nikunj Damani
  • 753
  • 3
  • 11
0

Converting Dictionary value to float

NSString *str = [dict objectForKey:@"key"];
CGFloat strFloat = (CGFloat)[str floatValue];
Chetan Rajauria
  • 209
  • 2
  • 4
0

Try this code, It will work for you. Tested!

float myFloatValue= 2345.678990;
NSString* formattedNumber = [NSString stringWithFormat:@"%.02f", myFloatValue];
NSLog(@"Changed Float Value:%@",formattedNumber);
Mukesh
  • 777
  • 7
  • 20