以编程方式查找iphone的区域设置货币

时间:2011-02-18 02:10:33

标签: iphone objective-c localization currency

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

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

7 个答案:

答案 0 :(得分:127)

在大多数情况下,货币符号是不够的。例如,在德国,我们写下这样的价格: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的价格和价格。

您将获得正确的格式化货币字符串,如下所示:

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的货币代码。澳元,美元,欧元等。

答案 1 :(得分:46)

我使用这些键从区域设置中提取货币符号/代码

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

答案 2 :(得分:5)

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

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

感谢。

答案 3 :(得分:5)

create macro first then use it
#define CURRENCY_SYMBOL [[NSLocale currentLocale] objectForKey:NSLocaleCurrencySymbol]

NSLog(@"%@ %.2f",CURRENCY_SYMBOL,25.50);

答案 4 :(得分:3)

马蒂亚斯·鲍奇迅速回答:

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

答案 5 :(得分:1)

谢谢你的回答。我终于发现我可以直接从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

答案 6 :(得分:0)

以下是Swift 5中的一个示例:

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