Find language currency for iphone programmatically

I want to find out the locale on a custom iphone programmatically. This means that if the user is in the US Store, the currency should be USD, for Australia - AUD. My goal of this task is to try to convert the price of the goods indicated in our application in accordance with the price requested by the AppStore.

For example, if we sell a video of $ 3, and an Australian wants to buy it, then I have to show 2.8 AUD on the screen of my application. This will reduce the calculation in the user at a real price in his country. Does anyone know how to do this?

+48
objective-c iphone currency localization
Feb 18 '11 at 2:10
source share
6 answers

In most cases, the currency symbol will not be enough. For example, in Germany we write our prices as follows: 1.99 €, but people in the USA use 1.99 US dollars. There are three differences in the line. Currency symbol, its position and separator.

If you want to do this correctly, you must use NSNumberFormatter. He takes care of all the differences between currency formats. And it is much better than you. Because he does it for all currencies, and not just for the 4 major currencies that 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 to buy an application, you cannot rely on the current locale of users, because you can use an account in the USA on a device with a German language standard. And the price of your product (actual price of 0.79 € in Germany) will show as 0.99 € (because it costs 0.99 US dollars). That would be wrong. You get the localized price already from the application store, you do not need to do the calculations yourself.
And you get a price and priceLocale for each of your SKProducts.

You will receive the correct formatted currency string as follows:

 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 requested a currency code.

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

+119
Feb 18 '11 at 9:10
source share
β€” -

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

 NSLocale *theLocale = [NSLocale currentLocale]; NSString *symbol = [theLocale objectForKey:NSLocaleCurrencySymbol]; NSString *code = [theLocale objectForKey:NSLocaleCurrencyCode]; 
+43
Feb 18 '11 at 5:29
source share

I used the code below in my application to extract the local curreny character and search for 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 

Thank.

+5
Dec 20 '11 at 11:03
source share
 create macro first then use it #define CURRENCY_SYMBOL [[NSLocale currentLocale] objectForKey:NSLocaleCurrencySymbol] NSLog(@"%@ %.2f",CURRENCY_SYMBOL,25.50); 
+4
Feb 06 '14 at 10:26
source share

Matthias Bauch replies promptly:

 var formatter = NSNumberFormatter() formatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle formatter.locale = product!.priceLocale var currencyString = "\(formatter.stringFromNumber(product!.price)!)" 
+2
Sep 21 '15 at 10:42
source share

thanks for your reply. I finally realized that I can get the price and 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 
+1
Feb 21 2018-11-21T00:
source share



All Articles