iphone - 在 iPhone 应用程序中本地化货币

标签 iphone objective-c localization currency

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

基本上,您输入 2 个本地货币的值,然后进行计算,然后用户会得到信息。

用户数字输入处理得很好。也就是说,在“编辑结束”时,执行一个方法,将数字转换为其等值的本地货币。因此,如果美国用户输入 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;
}

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

欧洲大陆国家不起作用。

任何有关如何解决此问题的建议都会很棒。

最佳答案

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

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

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

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

  • (NSString *)currencyDecimalSeparator
  • (NSString *)货币分组分隔符

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

希望对您有所帮助。

干杯,

凯文

关于iphone - 在 iPhone 应用程序中本地化货币,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1048598/

相关文章:

iphone - 动态本地化应用程序内部的 iOS 应用程序?

安卓和菜单本地化

iphone - 带有 subview 的可变 UITableCellView 高度

iphone - 常见 UI 字符串的本地化

ios - 如何使用从 AVCaptureDevice 捕获的 NSData 和 NSDictionary 组合一个面向 "right"的 UIImage?

objective-c - 更改 View 时如何保存数据

ios - NSNotificationCenter addObserver 被忽略

objective-c - 无法撤消多个操作

php - 在 Drupal 中如何获取 tnid 或翻译节点的节点 ID?

ios - iPhone 5S 的静态图像拍摄时间比 iPhone 5 慢?