Find locale currency for iphone programmatically
Asked Answered
H

7

55

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 AUD. My purpose of this task is to try to convert the item price listed on our app to be nearly match with the price that AppStore ask.

For example, if we sell a video 3 usd, and an Australian wants to buy it, then I should show 2.8 AUD in my app screen. It will reduce the calculation in the user over the real price in his country. Does anybody know how to do it?

Helwig answered 18/2, 2011 at 2:10 Comment(4)
+1, same question here. Anyone can help?Lucubration
+1, nice thing to know about.Seigel
#4428180Patmore
I think SKProduct has method priceLocale which returns NSLocale. I think you could/should use just that and show that to users...?Sharpeyed
C
128

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.

Cosmism answered 18/2, 2011 at 9:10 Comment(9)
I tried your first solution. It displays USD instead of INR(india). i don't understand, why?Gigi
Just a note to watch out for with this answer. Apple has added a Free Tier to IAP. If you use this you will get, for a USD currency locale, $0.00. Be sure to check the product.price for being 0 and handle that accordingly if you want it to say "Free" instead.Omeara
ISO 4217 tip is very useful!Solicitous
[NSLocale currentLocale] returns locale according to system settings? Like: I have locale set to English (America), thus it returns USD?Sharpeyed
This works for dollars but shows a (correctly positioned) weird circle for Euros. When I set currencyCode to "EUR" it shows the correct symbol... hmFlunky
Found the reason, it appears when setting a custom lang in the scheme... when run on device without custom lang the euro symbol appears correctly.Flunky
Watch out for precision loss, NSNumberFormatter will silently strip down NSDecimalNumbers into doubles. Any ideas to get around this?Horologist
Thank you so much for ISO 4217 Currency Code thing - works perfectly!Envoy
@BigMoney generatesDecimalNumbers (NSNumberFormatter) doesn't work?Heidt
M
47

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];
Mweru answered 18/2, 2011 at 5:29 Comment(1)
Thank you, hoping this will be helpful for the upcoming currency change in Croatia where we need to communicate the statutory fixed conversion rate of 7.53450 HRK = 1.00000 EUR. Will display if the locale starts with hr and/or the currency is HRK.Adventurer
I
5

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.

Incurve answered 20/12, 2011 at 11:3 Comment(3)
even if i am in india, it shows me $50 and en_Us. why is this ?Gigi
This will cause precision loss with decimals larger than doublesHorologist
@BigMoney indeed if the app handles amounts bigger than what can be formatted from a double.Hiramhirasuna
S
5
create macro first then use it
#define CURRENCY_SYMBOL [[NSLocale currentLocale] objectForKey:NSLocaleCurrencySymbol]

NSLog(@"%@ %.2f",CURRENCY_SYMBOL,25.50);
Slide answered 6/2, 2014 at 10:26 Comment(2)
use macro for such a simple task? really? messy code from the start?Terra
@Terra creating macro is a good use of programming. It reduces the messy of the code.Wilmer
T
3

Matthias Bauch answer in swift:

var formatter = NSNumberFormatter()
    formatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
    formatter.locale = product!.priceLocale
var currencyString = "\(formatter.stringFromNumber(product!.price)!)"
Teepee answered 21/9, 2015 at 10:42 Comment(1)
You don't need to add the (formatter.currencyCode) before the stringFromNumberPrimogenitor
H
1

thanks for your answer. I finally figured out that I can retrieve the price and the currency code directly from 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
Helwig answered 21/2, 2011 at 23:41 Comment(0)
N
0

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)
Natch answered 15/1, 2020 at 8:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.