Iphone – Why would this NSNumberFormatter not display all fractional digits

cocoa-touchiphoneuikit

I have a NSNumberFormatter instance like this:

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setAlwaysShowsDecimalSeparator:NO];
[formatter setAllowsFloats:YES];
[formatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[formatter setMinimumFractionDigits:0];
[formatter setNumberStyle:kCFNumberFormatterDecimalStyle];

Then I provide a value like this and print it:

NSNumber *number = [NSNumber numberWithFloat:4932.79661];
NSLog(@"%@", [formatter stringFromNumber:number]);

What I get is:

4,932.796

there are missing 2 digits. Now the strange thing is, if I make the number much bigger, i.e. 49328324.79661, I would get only 2 fractional digits. Any idea what might be wrong with the formatter? Must I explicitely tell it to use a specific precision?

Best Answer

Try setting:

[formatter setMaximumFractionDigits:20];

remember that floating point numbers might not be 100% accurately representable internally. So, for example, the output from your program above would be:

4,932.79638671875