Find locale currency for iphone programmatically

前端 未结 7 446
攒了一身酷
攒了一身酷 2020-12-02 05:20

I want to find out the currency locale on user\'s iphone programmatically. That means, if user is in US Store, the currency locale should be USD, for Australia, it should be

相关标签:
7条回答
  • 2020-12-02 05:59

    Matthias Bauch answer in swift:

    var formatter = NSNumberFormatter()
        formatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
        formatter.locale = product!.priceLocale
    var currencyString = "\(formatter.stringFromNumber(product!.price)!)"
    
    0 讨论(0)
  • 2020-12-02 06:00

    I used these keys to extract currency symbols/codes from locales

    NSLocale *theLocale = [NSLocale currentLocale];
    NSString *symbol = [theLocale objectForKey:NSLocaleCurrencySymbol];
    NSString *code = [theLocale objectForKey:NSLocaleCurrencyCode];
    
    0 讨论(0)
  • 2020-12-02 06:00
    create macro first then use it
    #define CURRENCY_SYMBOL [[NSLocale currentLocale] objectForKey:NSLocaleCurrencySymbol]
    
    NSLog(@"%@ %.2f",CURRENCY_SYMBOL,25.50);
    
    0 讨论(0)
  • 2020-12-02 06:01

    I used below code in my app to retrieve local curreny sign and find the delimiters. I will help you,

    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
    

    Thanks.

    0 讨论(0)
  • 2020-12-02 06:05

    Here is an example in Swift 5:

    let formatter = NumberFormatter()
    formatter.formatterBehavior = .behavior10_4
    formatter.numberStyle = .currency
    formatter.locale = product.priceLocale
    print(formatter.string(from: products![0].price)
    
    0 讨论(0)
  • 2020-12-02 06:07

    In most cases the currency symbol won't be enough. For example, in Germany we write our prices like this: 1,99€ but people in the US use $1.99. There are three differences in the string. The currency symbol, the position of it and the separator.

    If you want to do it right you should use a NSNumberFormatter. It takes care of all the differences between currency formats. And it does it much better than you. Because it does it for all currencies, not just for the 4 main currencies you want to support.

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

    If you want to use this for in app purchase you can't rely on the users current locale, because it is possible to use a US-based account on a device with a DE (german) locale. And the price of your item (actual price is 0,79€ in Germany) would show as 0,99€ (because it costs $0.99 in the US). This would be wrong. You get a localized price already from the app store, there is no need to do calculations on your own.
    And you get a price and a priceLocale for each of your SKProducts.

    You would get the correct formatted currency string like this:

    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];
    

    EDIT: since you specifically asked for the currency code.

    You can get it with NSString *currencyCode = [formatter currencyCode]; This will give you the currency code according to ISO 4217. AUD, USD, EUR and so on.

    0 讨论(0)
提交回复
热议问题