【问题标题】:Find locale currency for iphone programmatically以编程方式查找 iphone 的语言环境货币
【发布时间】:2011-06-29 13:09:20
【问题描述】:

我想以编程方式找出用户 iphone 上的货币区域设置。这意味着,如果用户在美国商店,货币区域设置应该是美元,对于澳大利亚,它应该是澳元。我这个任务的目的是尝试将我们应用程序上列出的项目价格转换为与 AppStore 要求的价格几乎匹配。

例如,如果我们出售 3 美元的视频,而澳大利亚人想购买它,那么我应该在我的应用屏幕上显示 2.8 澳元。它将减少用户对其国家实际价格的计算。有人知道怎么做吗?

【问题讨论】:

  • +1,同样的问题。有人可以帮忙吗?
  • +1,好消息。
  • 我认为 SKProduct 具有返回 NSLocale 的方法 priceLocale。我认为您可以/应该使用它并向用户展示...?

标签: iphone objective-c localization currency


【解决方案1】:

在大多数情况下,货币符号是不够的。例如,在德国,我们这样写价格:1,99 欧元,但美国人使用 1.99 美元。字符串有三个不同之处。货币符号、它的位置和分隔符。

如果你想正确地做,你应该使用 NSNumberFormatter。它处理货币格式之间的所有差异。它比你做得更好。因为它适用于所有货币,而不仅仅是您想要支持的 4 种主要货币。

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

如果您想将此用于应用内购买,则不能依赖用户当前的语言环境,因为可以在具有 DE(德国)语言环境的设备上使用基于美国的帐户。您的商品价格(德国的实际价格为 0.79 欧元)将显示为 0.99 欧元(因为它在美国的价格为 0.99 美元)。这是错误的。您已从应用商店获得本地化价格,无需自行计算。
您会获得每个 SKProducts 的价格和 priceLocale。

你会得到正确格式的货币字符串,如下所示:

SKProduct *product = [self.products objectAtIndex:indexPath.row];
NSNumberFormatter *formatter = [[[NSNumberFormatter alloc] init] autorelease];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[formatter setLocale:product.priceLocale];
currencyString = [formatter stringFromNumber:product.price];

编辑:因为您特别要求提供货币代码。

您可以使用NSString *currencyCode = [formatter currencyCode]; 获取它,这将为您提供符合 ISO 4217 的货币代码。澳元、美元、欧元等。

【讨论】:

  • 我尝试了您的第一个解决方案。它显示美元而不是 INR(印度)。我不明白,为什么?
  • 这个答案只是一个需要注意的注释。 Apple 为 IAP 添加了免费套餐。如果您使用它,对于美元货币语言环境,您将获得 0.00 美元。请务必检查 product.price 是否为 0,如果您希望它改为“免费”,请相应处理。
  • ISO 4217 提示非常有用!
  • [NSLocale currentLocale] 根据系统设置返回语言环境?喜欢:我将语言环境设置为英语(美国),因此它返回美元?
  • 这适用于美元,但显示了一个(正确定位的)欧元奇怪的圆圈。当我将currencyCode 设置为“EUR”时,它会显示正确的符号……嗯
【解决方案2】:

我使用这些键从语言环境中提取货币符号/代码

NSLocale *theLocale = [NSLocale currentLocale];
NSString *symbol = [theLocale objectForKey:NSLocaleCurrencySymbol];
NSString *code = [theLocale objectForKey:NSLocaleCurrencyCode];

【讨论】:

    【解决方案3】:

    我在我的应用程序中使用以下代码来检索本地货币符号并找到分隔符。我会帮你的,

    NSDecimalNumber *amount = [NSDecimalNumber decimalNumberWithString:@"50.00"];
    NSNumberFormatter *currencyFormat = [[NSNumberFormatter alloc] init];
    NSLocale *locale = [NSLocale currentLocale];
    [currencyFormat setNumberStyle:NSNumberFormatterCurrencyStyle];
    [currencyFormat setLocale:locale];
    NSLog(@"Amount with symbol: %@", [currencyFormat stringFromNumber:amount]);//Eg: $50.00
    NSLog(@"Current Locale : %@", [locale localeIdentifier]);//Eg: en_US
    

    谢谢。

    【讨论】:

    • 即使我在印度,它也会显示 50 美元和 en_Us。这是为什么?
    • 这将导致小数大于双精度的精度损失
    • @BigMoney 确实,如果应用程序处理的金额大于可以从双精度格式化的金额。
    【解决方案4】:
    create macro first then use it
    #define CURRENCY_SYMBOL [[NSLocale currentLocale] objectForKey:NSLocaleCurrencySymbol]
    
    NSLog(@"%@ %.2f",CURRENCY_SYMBOL,25.50);
    

    【讨论】:

    • 使用宏来完成如此简单的任务?真的吗?一开始就乱码?
    • @gaussblurinc 创建宏是一个很好的编程用途。它减少了代码的混乱。
    【解决方案5】:

    Matthias Bauch 迅速回答:

    var formatter = NSNumberFormatter()
        formatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
        formatter.locale = product!.priceLocale
    var currencyString = "\(formatter.stringFromNumber(product!.price)!)"
    

    【讨论】:

    • stringFromNumber 前不需要加 (formatter.currencyCode)
    【解决方案6】:

    感谢您的回答。我终于想通了,我可以直接从 Apple 检索价格和货币代码:

    - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {    
        NSArray *products = response.products;
        if (products && products.count != 0) {
            product = [products objectAtIndex:0];
            [[NSNotificationCenter defaultCenter] postNotificationName:PRICE_UPDATED object:product.LocalizedPrice];    
        } 
    
        // finally release the reqest we alloc/init’ed in requestProUpgradeProductData
        [productsRequest release];
    }
    
    
    
    @implementation SKProduct (LocalizedPrice)
    
    - (NSString *)LocalizedPrice
    {
        NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
        [numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
        [numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
        [numberFormatter setLocale:self.priceLocale];
        NSString *formattedString = [numberFormatter stringFromNumber:self.price];
        [numberFormatter release];
        return formattedString;
    }
    
    @end
    

    【讨论】:

      【解决方案7】:

      这是 Swift 5 中的一个示例:

      let formatter = NumberFormatter()
      formatter.formatterBehavior = .behavior10_4
      formatter.numberStyle = .currency
      formatter.locale = product.priceLocale
      print(formatter.string(from: products![0].price)
      

      【讨论】:

        猜你喜欢
        • 2012-09-23
        • 2011-12-13
        • 1970-01-01
        • 2011-10-07
        • 1970-01-01
        • 2021-06-08
        • 1970-01-01
        • 2011-06-26
        • 1970-01-01
        相关资源
        最近更新 更多