stonedauwg
stonedauwg

Reputation: 1407

NSDecimalNumber vs NSNumberFormatter numbers from same invariant string produce diff results

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

Upvotes: 1

Views: 78

Answers (0)

Related Questions