2

我正在测试我的应用程序。一切正常,除非我将语言环境更改为德国。

基本上,您以当地货币输入 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;
}

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

欧洲大陆国家不工作。

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

4

1 回答 1

2

鉴于该代码适用于美国、英国、日本和爱尔兰,但不适用于欧洲大陆(德国),我会检查您如何处理千位和小数点分隔符。

也就是说,在适用于您的代码的国家/地区,千位分隔符是逗号,小数点是点(句号)。所以 10000 美元和 50 美分就是 10,000.50。

在欧洲大陆(德国等),千位分隔符是点(句号),十进制分隔符是逗号。因此,德国的上述值将是 10.000,50

NSNumberFormatter 有两种您可能想要查看的方法:-

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

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

我希望这会有所帮助。

干杯,

凯文

于 2009-07-26T09:35:30.787 回答