views:

546

answers:

1

as a quick test of formatting very large, precise numbers i'm doing:

(number is a synthesized NSDecimalNumber...)
number = [[NSDecimalNumber alloc] initWithDecimal:[[NSDecimalNumber numberWithDouble:420000008698643507200000.0F] decimalValue]];
DLog(@"unformatted: %@", [number stringValue]);

which gives me: "unformatted: 420000008698643507200000"

but when i try to format it via NSNumberFormatter, it's quite off... rounding way up or something...

  numFormatter = [[NSNumberFormatter alloc] init];
  [numFormatter setNumberStyle:kCFNumberFormatterDecimalStyle];
  [numFormatter setRoundingMode:kCFNumberFormatterRoundDown];
  [numFormatter setMaximum:nil];

  DLog(@"formatted: %@", [numFormatter stringFromNumber:number]);

gives me: "formatted: 420,000,008,698,644,000,000,000"

i'm hoping there's some secret NSNumberFormatter magic property that i'm not seeing, but i tried setUsesSignificantDigits and setMaximumSignificantDigits, but that didn't seem to work.

anyone have any ideas? thanks a bunch!

+1  A: 

Your loss of precision comes from the fact that NSNumberFormatter converts the number to a double.

Have you considered using NSDecimalNumber's descriptionWithLocale: method? As a starting point you could just pass in [NSLocale currentLocale] as a param. Then you can start playing with the locale configuration via NSLocaleDecimalSeparator and NSLocaleGroupingSeparator.

nicktmro