Mingming
Mingming

Reputation: 2219

NSNumberFormatter only formats up to 14 significant digits

I saw some code trying to format lat long using the following NSNumberFormatter.

NSNumberFormatter * sut = [[NSNumberFormatter alloc] init];
[sut setMaximumFractionDigits:20]; // also tried set to 15 or 16, not working too.
[sut setMaximumSignificantDigits:20];

NSLocale *enUSPOSIXLocale = [[NSLocale alloc]initWithLocaleIdentifier:@"en_US_POSIX"];
[sut setLocale:enUSPOSIXLocale];

NSString *expected = @"1.299129258067496"; // 16 significants digits
NSString *actual = [sut stringFromNumber:@(1.299129258067496)];

[[actual should] equal:expected];// Failed to pass, the actual is @"1.2991292580675"

Although in this case, we may not need to use the NSNumberFormatter to get the correct result, I'm wondering why NSNumberFormatter only returns string of up to 14 significant digits.

Upvotes: 4

Views: 75

Answers (1)

Simon Acker
Simon Acker

Reputation: 11

It only shows 14 decimal places because the double type rounds at 15 decimal places. This worked for me because you can set the number of decimal places shown.

[NSString stringWithFormat:@"%.20f", 1.299129258067496]

Just make sure the number of decimal places does not exceed the number otherwise the program makes up numbers to fill the rest. Hope this helps.

Upvotes: 1

Related Questions