Reputation: 1
I'm working on an app for iOS and I'm trying to determine the user's local currency through the currency code provided by NSNumberFormatter. The question I have though is how does it work? Is it using the phone's setting's to determine the local currency or is it contacting the iOS App Store?
The iOS Developer Library documentation just explains what it will return back but not how it's determined.
Upvotes: 0
Views: 1119
Reputation: 4043
Hope it helps you
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[formatter setLocale:[NSLocale currentLocale]];
NSString *strLocalizedMoney = [formatter stringFromNumber:myCurrencyNSNumberObject];
if you have to change curreny/location then you have to use NSLocale
NSLocale* japanese_Locale = [[NSLocale alloc] initWithLocaleIdentifier:@"ja_JP"]; //change identifier as per your requirement
NSNumberFormatter* formater = [[NSNumberFormatter alloc] init];
[formater setNumberStyle:NSNumberFormatterCurrencyStyle];
[formater setLocale:japanese_Locale];
Full example i.e.
NSLocale* japanese_Locale = [[NSLocale alloc] initWithLocaleIdentifier:@"ja_JP"]; //change identifier as per your requirement
NSNumberFormatter* formater = [[NSNumberFormatter alloc] init];
[formater setNumberStyle:NSNumberFormatterCurrencyStyle];
[formater setLocale:japanese_Locale];
// Local currency symbol added here
NSString* yourCurrencySymbol = [formater currencySymbol];
NSLog( @"%@", yourCurrencySymbol ); // Prints '¥' or Any if Changes
// International currency symbol
NSString* internationalCurrencySymbol = [formater internationalCurrencySymbol];
NSLog( @"%@", internationalCurrencySymbol ); // Prints 'JPY' it is international symbol
Upvotes: 1
Reputation: 1845
NSNumberFormatter provides you the currency code based on your locale and it is also change able by setting locale for number formatter like this.
numberFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
It would probably be best to not set the currencySymbol at all. Then when the locale is, say, China (@"zh_CN"), you get
Further you can get the currency related properties on nsnumber like this.
numberFormatter.internationalCurrencySymbol
numberFormatter.currencySymbol
numberFormatter.currencyCode
Upvotes: 3