views:

926

answers:

1

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.

+1  A: 

Given the code works for the US, UK, Japan and Ireland but not mainland Europe (Germany) I would check how you are handling the thousands and the decimal seperator.

That is, in the countries which work for your code the thousands seperator is the comma and the decimal is the point (full stop). So 10000 dollars and 50 cents would be 10,000.50.

In mainland Europe (Germany etc) the thousands seperator is the point (full stop) and the decimal seperator is the comma. So the above value in Germany would be 10.000,50

NSNumberFormatter has two methods you might want to look at:-

  • (NSString *)currencyDecimalSeparator
  • (NSString *)currencyGroupingSeparator

You can find a list of countries in WIKI (http://en.wikipedia.org/wiki/Decimal_separator) for each of the formats and test to see if your problem is for one group only or not.

I hope that helps.

Cheers,

Kevin

Kevin Horgan