Objective-c – How to prevent iPhone NSNumberFormatter from rounding

cocoa-touchiphoneobjective-c

I'm trying to format large currency numbers like this:

NSNumber *testVal = [NSDecimalNumber decimalNumberWithString: @"999999999999999993.00"];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[formatter setMaximumIntegerDigits:19];
[formatter setMaximumFractionDigits:0];
NSNumberFormatter *formatter = [Formatters numberFormatterForField:f];
NSLog([formatter stringFromNumber:testVal]);

Ignoring the effect of Locale for a moment, I find that I consistently get the following output regardless of the rounding:

1,000,000,000,000,000,000

I expect:

999,999,999,999,999,993

I've tried changing the rounding with no effect. Why does NSNumberFormatter insist on rounding up?

Best Solution

NSNumberFormatter converts the number to a double before displaying, so that's not what you want. NSDecimalNumber has a descriptionWithLocale: method that should suit your needs.

See this answer: link