My app uses monetary values, and I would like to represent these values in the currency and formats that the user has set on their iPhone however, when testing, I seem to be having troubles with the German EURO format.
I have a method that returns a decimal number (for storing in core data) from the currency formatted string. The string was actually formatted by another (similar) method using NSlocale.
I have tested with UK, US, Japanese and German locales, so far only Germany has caused any problems.
Here's the code, the string being passed is for example: 2,56 €
+ (NSDecimalNumber*)TLCurrencyFormatFromString:(NSString*)strNumber {
NSNumberFormatter *fmtrCurrencyFromStr = nil;
fmtrCurrencyFromStr = [[NSNumberFormatter alloc] init];
[fmtrCurrencyFromStr setFormatterBehavior:NSNumberFormatterBehavior10_4];
[fmtrCurrencyFromStr setLocale:[NSLocale currentLocale]];
[fmtrCurrencyFromStr setNumberStyle:NSNumberFormatterCurrencyStyle];
[fmtrCurrencyFromStr setGeneratesDecimalNumbers:YES];
NSLog(@"Currency Separator: %@", [fmtrCurrencyFromStr decimalSeparator]); //prints a comma
NSLog(@"Currency Code: %@", [fmtrCurrencyFromStr currencyCode]); //prints EUR
int currencyScale = [fmtrCurrencyFromStr maximumFractionDigits];
NSDecimalNumberHandler *roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain scale:currencyScale raiseOnExactness:FALSE raiseOnOverflow:TRUE raiseOnUnderflow:TRUE raiseOnDivideByZero:TRUE];
NSDecimalNumber* currencyNumber = [[NSDecimalNumber alloc] initWithDecimal:[[fmtrCurrencyFromStr numberFromString:strNumber] decimalValue]];
NSLog(@"Currency Number = %@", [currencyNumber decimalNumberByRoundingAccordingToBehavior:roundingBehavior]);
return [currencyNumber decimalNumberByRoundingAccordingToBehavior:roundingBehavior];
}
The 'currencyNumber' variable gets printed as: -18446912453607470532000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
whereas in all the other locales, it would print 2.56 for storing into Core Data as NSDecimalNumber.
Setting the property:
[fmtrCurrencyFromStr setLenient:YES];
Fixes the problem for France, Germany, Norway, UK, US, Japan, Nambia etc... I can't say it fixes it for all because I haven't tested all, but of all those that I've tested so far, it has worked.