iOS NSNumberFormatter货币代码如何运作?

时间:2016-02-04 06:53:12

标签: ios locale currency nsnumberformatter

我正在开发适用于iOS的应用,并且我试图通过NSNumberFormatter提供的货币代码来确定用户的本地货币。我的问题是它是如何工作的?是使用手机的设置来确定本地货币还是联系iOS App Store?

iOS Developer Library文档只解释了它将返回的内容,而不是它如何确定。

2 个答案:

答案 0 :(得分:3)

NSNumberFormatter根据您的语言环境为您提供货币代码,并且还可以通过设置数字格式化程序的区域设置来更改。

numberFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];

最好不要设置currencySymbol。然后当语言环境是中国(@“zh_CN”)时,你得到

此外,您可以在nsnumber上获取与此相关的货币相关属性。

numberFormatter.internationalCurrencySymbol   
numberFormatter.currencySymbol                
numberFormatter.currencyCode  

答案 1 :(得分:1)

希望它可以帮到你

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[formatter setLocale:[NSLocale currentLocale]];
NSString *strLocalizedMoney = [formatter stringFromNumber:myCurrencyNSNumberObject];

如果您必须更改curreny / location,则必须使用NSLocale

 NSLocale* japanese_Locale = [[NSLocale alloc] initWithLocaleIdentifier:@"ja_JP"]; //change identifier as per your requirement
NSNumberFormatter* formater = [[NSNumberFormatter alloc] init];
[formater setNumberStyle:NSNumberFormatterCurrencyStyle];
[formater setLocale:japanese_Locale];

完整的例子,即

NSLocale* japanese_Locale = [[NSLocale alloc] initWithLocaleIdentifier:@"ja_JP"]; //change identifier as per your requirement
NSNumberFormatter* formater = [[NSNumberFormatter alloc] init];
[formater setNumberStyle:NSNumberFormatterCurrencyStyle];
[formater setLocale:japanese_Locale];

//  Local currency symbol added here
NSString* yourCurrencySymbol = [formater currencySymbol];
NSLog( @"%@", yourCurrencySymbol ); // Prints '¥' or Any if Changes

// International currency symbol
NSString* internationalCurrencySymbol = [formater internationalCurrencySymbol];
NSLog( @"%@", internationalCurrencySymbol ); // Prints 'JPY' it is international symbol