I am testing my app. All is working fine except when I change locales to Germany.
Basically you input 2 values in your local currency, a calculation happens and the user gets info back.
Users numeric inputs are handled well. That is, on "Editing Did End" a method executes that converts the number to its local currency equivalent. So if US users enters 10000 they will be returned $10,000.00. Here's the code:
- (NSMutableString *) formatTextValueToCurrency: (NSMutableString *) numberString {
NSNumber *aDouble = [NSNumber numberWithFloat: [numberString floatValue]];
NSMutableString *aString = [NSMutableString stringWithCapacity: 20];
NSLocale *theLocale;
NSNumberFormatter *currencyStyle = [[NSNumberFormatter alloc] init];
[currencyStyle setFormatterBehavior:NSNumberFormatterBehavior10_4];
[currencyStyle setNumberStyle:NSNumberFormatterCurrencyStyle];
theLocale = [NSLocale currentLocale];
[currencyStyle setLocale: theLocale];
[aString appendString: [currencyStyle stringFromNumber:aDouble]];
[currencyStyle release];
return aString;
}
However a problem occurs when I want to process the above currency values to get the user his/her info. That is, the app will now needs to get 10000 from $10,000.00 (or whatever currency) to send into the calculation method. Here's the code:
- (float) getValueFromCurrency: (id) sender {
NSNumber *aDouble = [NSNumber numberWithFloat: 0.0];
UITextField *textField = (UITextField *) sender;
NSMutableString *aString= [NSMutableString stringWithCapacity: 20];
NSLocale *theLocale;
float result;
NSNumberFormatter *currencyStyle = [[NSNumberFormatter alloc] init];
[currencyStyle setFormatterBehavior:NSNumberFormatterBehavior10_4];
[currencyStyle setNumberStyle:NSNumberFormatterCurrencyStyle];
theLocale = [NSLocale currentLocale];
[currencyStyle setLocale: theLocale];
NSLog(@"The locale is %@", currencyStyle.locale.localeIdentifier);
//Above NSLog looks good because it returns de_DE
[aString appendString: textField.text];
//The append from text field to string is good also
aDouble = [currencyStyle numberFromString: aString];
//For some reason, nil is returned
result = [aDouble floatValue];
[currencyStyle release];
return result;
}
For some reason the US, UK, Japanese and Irish locales are fine.
Continental European countries are not working.
Any advice on how to fix this would be great.