1

Not understanding why using NSNumberFormatter vs NSDecimalNumber methods to convert strings to decimal numbers are producing different results.

NSNumberFormatter *customNumFmt = [[NSNumberFormatter alloc] init];
customNumFmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
customNumFmt.generatesDecimalNumbers = YES;
customNumFmt.numberStyle = NSNumberFormatterDecimalStyle;
NSDecimalNumber *result = [customNumFmt numberFromString:@"76.99"];

result, as output with standard NSObject description method, will be 76.98999999999999

Using the same kind of logic with the NSDecimalNumber helper method:

NSDecimalNumber *result = [NSDecimalNumber decimalNumberWithString:@"76.99" locale:[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]];

result, as output with standard NSObject description method, will be 76.99

stonedauwg
  • 1,328
  • 1
  • 14
  • 35

0 Answers0