NSDecimalNumber vs NSNumberFormatter numbers from same invariant string produce diff results

78 views Asked by At

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

0

There are 0 answers