2009-06-26 62 views
2

我正在测试我的应用。除了当我将语言环境更改为德国时,所有工作都正常。本地化iphone应用货币

基本上你用你的本地货币输入2个值,计算发生,用户获得信息。

用户数字输入处理得很好。也就是说,在“Editing Did End”上执行一个方法,将该数字转换为等值的本地货币。所以如果美国用户输入10000,他们将被退回$ 10,000.00。代码如下:

- (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; 

} 

但是,当我想处理上述货币值以获取用户他/她的信息时发生问题。也就是说,应用程序现在需要从$ 10,000.00(或任何货币)中获得10000以发送到计算方法中。这里是代码:

- (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; 
} 

由于某些原因,美国,英国,日本和爱尔兰的语言环境都很好。

欧洲大陆国家无法使用。

任何关于如何解决这个问题的建议将会很棒。

回答

2

鉴于代码适用于美国,英国,日本和爱尔兰,但不是欧洲大陆(德国),我会检查你是如何处理数千和十进制分隔符的。

也就是说,在为你的代码工作的国家里,千位分隔符是逗号,小数点就是点(句号)。所以10000美元和50美分就是10,000.50美元。

在欧洲大陆(德国等),千分位是点(句号),小数点位置是逗号。因此,在德国上面的值,将10.000,50

NSNumberFormatter有两个方法,你可能想看看: -

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

对于每种格式,您都可以在WIKI(http://en.wikipedia.org/wiki/Decimal_separator)中找到一个国家/地区的列表,然后测试以确定您的问题是否仅针对一个群组。

我希望有帮助。

干杯,

凯文