【问题标题】:iOS NSNumberFormatter currency code how does it work?iOS NSNumberFormatter 货币代码是如何工作的?
【发布时间】:2016-02-04 06:53:12
【问题描述】:

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

iOS 开发者库文档只是解释了它会返回什么,而不是它是如何确定的。

【问题讨论】:

  • 你明白我的回答吗?

标签: ios locale currency nsnumberformatter


【解决方案1】:

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

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

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

您还可以像这样在 nsnumber 上获取与货币相关的属性。

numberFormatter.internationalCurrencySymbol   
numberFormatter.currencySymbol                
numberFormatter.currencyCode  

【讨论】:

    【解决方案2】:

    希望对你有帮助

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

    如果您必须更改货币/位置,则必须使用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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多