I'm using NSNumberFormatter to get the object value of a string currency. No matter what combination of settings I try, I always get weird precision errors.
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle: NSNumberFormatterCurrencyStyle];
[numberFormatter setGeneratesDecimalNumbers:YES];
[numberFormatter setMaximumFractionDigits:2];
[numberFormatter setMinimumFractionDigits:2];
[numberFormatter setRoundingIncrement:@(0.01)];
NSDecimalNumber *value;
[numberFormatter getObjectValue:&value forString:@"$9.95" errorDescription:nil];
NSLog(@"value is %@ class %@", value,[value class]);
Produces:
2014-08-03 19:25:17.640 Test[50776:60b] value is 9.949999999999999 class NSDecimalNumber
It appears that minFractionDigits, maxFractionDigits and roundingIncrement are only relevent in the context of formatting a value to a string, not converting a string to a number. What's even more baffling to me is that NSNumberFormatter is using NSDecimalNumber which is supposed to avoid these types of precision issues.
Am I missing something completely obvious here?