0

NSString is @"16.240" it's doubleValue is 16.239999999999998

What happened behind the scenes?

Gon
  • 1,135
  • 12
  • 22

2 Answers2

0

Because of floating point rounding errors.

Long-story-short; not every number can be represented using floating point types.

References: #1, #2, #3.

Community
  • 1
  • 1
Droppy
  • 9,691
  • 1
  • 20
  • 27
0

If it is value for price (or money in general) better to use NSDecimalNumber because it is save each part of float value as is.

NSDecimalNumber *price = [NSDecimalNumber decimalNumberWithString:@"16.240"];
NSLog(price); //16.24

http://rypress.com/tutorials/objective-c/data-types/nsdecimalnumber

Andrew Bogaevskyi
  • 2,251
  • 21
  • 25