iPhone: How to get local currency symbol (i.e. "$" instead of "AU$")
Asked Answered
B

7

30

Here's a code of how I get currency symbol now:

NSLocale *lcl = [[[NSLocale alloc] initWithLocaleIdentifier:@"au_AU"] autorelease];
NSNumberFormatter *fmtr = [[[NSNumberFormatter alloc] init] autorelease];
[fmtr setNumberStyle:NSNumberFormatterCurrencyStyle];
[fmtr setLocale:lcl];

NSLog( @"%@", [lcl displayNameForKey:NSLocaleCurrencySymbol value:@"AUD"] );
NSLog( @"%@", [fmtr currencySymbol] );

Both NSLogs return "AU$". As I understood from Apple development documentation, there are at least two currency symbols for each currency (these symbols could be the same, though) - local (that is used within a country. $ for Australia, for example) and international (AU$ for Australia). So, the question is how to get LOCAL currency symbol. Any ideas?

Thanks in advance.

Bartolomeo answered 13/12, 2010 at 10:4 Comment(3)
Maybe that's actually a bug (i.e. oversight of the localizing team) in iOS. Have you tried the same with other locales, e.g. USA? If you get there once "$" and once "US$", then I'd consider that a bug and file it at bugreport.apple.com, and add a special handling for this AU$ to your code.Kokoruda
Yep - definitely a bug. I tried this and en_US returns $Silvers
@Thomas Tempelmann: I've just filed a bug. Thanks.Bartolomeo
S
10

Your code should work, however the locale identifier is wrong. It should be "en_AU".

See "Using the Locale Object" in the "Internationalization and Localization Guide" (https://developer.apple.com/library/ios/documentation/MacOSX/Conceptual/BPInternational/InternationalizingLocaleData/InternationalizingLocaleData.html#//apple_ref/doc/uid/10000171i-CH13-SW4)

Sissified answered 25/11, 2011 at 4:27 Comment(2)
Holy c... Thank you man :). I solved this problem in other way, but this really works, I've just checked :).Bartolomeo
NSString *currencySymbol = [[NSLocale currentLocale] objectForKey:NSLocaleCurrencySymbol];Eglantine
F
21
NSNumberFormatter *currencyFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[currencyFormatter setLocale:[NSLocale currentLocale]];
[currencyFormatter setMaximumFractionDigits:2];
[currencyFormatter setMinimumFractionDigits:2];
[currencyFormatter setAlwaysShowsDecimalSeparator:YES];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];

NSNumber *someAmount = [NSNumber numberWithFloat:5.00];    
NSString *string = [currencyFormatter stringFromNumber:someAmount];

You will receive $5.00 for US, ¥5.00 for Japan, 5.00€ for Europe, etc.

Fierce answered 16/6, 2012 at 9:38 Comment(3)
The question was about specific locales, not "[NSLocale currentLocale]". Anyways, Nik provided correct answer a couple of months ago. Thanks.Bartolomeo
¥5.00 for Japan is frankly wrong. 5 Yen is about 2 1/2 cents. You don't display Yen with two decimals. $5.00 would be about 1,000 Yen, displayed as ¥1,000 and not ¥1,000.00. The locale knows how to print Yen properly.Amalberga
Fixed my issue, in my case i had to get currency for my iNApp Product so I replaced [currencyFormatter setLocale:[NSLocale currentLocale]]; with [currencyFormatter setLocale:_product.priceLocale];Cudweed
K
12

This snippet returns the currency symbol ¥ for locale "ja_JP" (could be any other locale).

NSLocale* japanese_japan = [[[NSLocale alloc] initWithLocaleIdentifier:@"ja_JP"] autorelease];
 NSNumberFormatter* fmtr = [[[NSNumberFormatter alloc] init] autorelease];
 [fmtr setNumberStyle:NSNumberFormatterCurrencyStyle];
 [fmtr setLocale:japanese_japan];

 // Local currency symbol (what you're asking for)
 NSString* currencySymbol = [fmtr currencySymbol];
 NSLog( @"%@", currencySymbol ); // Prints '¥'

 // International currency symbol 
 NSString* internationalCurrencySymbol = [fmtr internationalCurrencySymbol];
 NSLog( @"%@", internationalCurrencySymbol ); // Prints 'JPY'

It's unfortunate that for au_AU you get AU$ as the local currency symbol instead of just $, but that must be the way it's meant to be displayed on iOS. However note that the international symbol printed for au_AU is not AU$ but AUD.

Kayo answered 15/12, 2010 at 15:49 Comment(1)
Yeah, that's weird because JPY (and AUD either) are not international currency symbols. These are currency codes (you can get them by [fmtr currencyCode]). Thanks for help, but it's not exactly what I was looking for.Bartolomeo
S
10

Your code should work, however the locale identifier is wrong. It should be "en_AU".

See "Using the Locale Object" in the "Internationalization and Localization Guide" (https://developer.apple.com/library/ios/documentation/MacOSX/Conceptual/BPInternational/InternationalizingLocaleData/InternationalizingLocaleData.html#//apple_ref/doc/uid/10000171i-CH13-SW4)

Sissified answered 25/11, 2011 at 4:27 Comment(2)
Holy c... Thank you man :). I solved this problem in other way, but this really works, I've just checked :).Bartolomeo
NSString *currencySymbol = [[NSLocale currentLocale] objectForKey:NSLocaleCurrencySymbol];Eglantine
T
8

It's not ideal in that it's not coming out of the system, but obviously you could create your own internal table using a list of current currency symbols*. Since that list has the unicode symbols for it it would simply be a matter of matching up the Apple list of locales with the list.

Y'know, just in case the Apple-provided ones aren't actually accessible.

*Note: link not intended to be authoritative, see comments.

Treviso answered 15/12, 2010 at 14:14 Comment(5)
Yeah, sure, I was thinking about that, but that's what I hoped to avoid. Well, if there will be no better solution, I shall use this. Thanks.Bartolomeo
Totally unrelated - but I recognize your avatar icon. I know the Carcassonne developers, they're right here in the same town as I live. They're nice guys, and I'm so happy they took on this job.Kokoruda
They did an excellent job with the iOS port, highly commendable. It's one of the top two or three boardgame-to-app conversions so far.Treviso
Your link is not right in some occasions. The OP is searching for the local currency symbol. But for Switzerland it's wrong. It says CHF, while locally it's abbreviated to Fr. or SFr. So the links is not really reliable.Becerra
Ah, alas. I simply pointed to the first Google result, I definitely don't mean to recommend it as an authoritative source. Sorry if I implied it was valid.Treviso
H
2

Swift version, code adapted from Valentyn's answer (tested on Swift 4.1):

func formattedCurrency(amount: Double, locale: Locale) -> String? {
    let formatter = NumberFormatter()
    formatter.locale = locale
    formatter.maximumFractionDigits = 2
    formatter.minimumFractionDigits = 2
    formatter.alwaysShowsDecimalSeparator = true
    formatter.numberStyle = .currency
    return formatter.string(from: NSNumber(value: amount))
}

It gives a decent result, although it doesn't change the position of the currency symbol correctly for all locales, despite what is written in the original answer. But that's the way Apple wants it to be, so be it.

The Australian dollar is displayed as the original question enquires, as "$1.50".

Hepza answered 22/4, 2018 at 17:24 Comment(0)
S
1

You could, If you need to, create a .strings file that contains the currency, and use the NSLocalizedString function for creating the localized currency. Something like this:

en.lproj
myApp.strings:

"currencySymbol"="$"
"currencyFormat"="$%lf"

au_AU.lproj
myApp.strings:

"currencySymbol"="$"
"currencyFormat"="$%lf"

ja_JP.lproj
myApp.strings:

"currencySymbol"="¥"
"currencyFormat"="¥%lf"

And use that like this:

NSString *money = [NSString stringWithFormat:@"%@%lf", NSLocalizedString:(@"currencySymbol"), myMoney];

However, that means that for every localization you support you need a .strings file. Also, this means that for some localizations, the currency symbol wouldn't be enough to display the proper monetary format, you would need to use something like this:

NSString *money = [NSString stringWithFormat:NSLocalizedString(@"CurrencyFormat"), myMoney];

This has some limitations, but it just might work for you.

Sessoms answered 17/12, 2010 at 14:4 Comment(1)
Thanks for the solution. It might work, but for now much easier looks the solution from Matthew Frederick - I already have a table of currencies in my database, so I'll just add a column with local currency symbol. This looks easier in my situation. Looks there's no built-in solution.Bartolomeo
S
0

Could you could get the current string and strip it of all a-Z characters? If the resulting string has length > 0 then you've got your symbol. Otherwise use the original string.

It's tacky and I'm not sure if this would work for all currencies worldwide but it might work?

Silvers answered 20/12, 2010 at 4:55 Comment(5)
Nope, this won't work. Here's a list of circulating currencies: ฿ · Br · ₵ · ¢ · C$ · ₡ · B/. · ден. · ₫ · € · ƒ · Ft · ₲ · Kč · ₭ · L · £ / ₤ · ₥ · ₦ · ₱ · P · R · RM · RSD · · ₨ · р. · S/. · ৳ · R$ · $ · · · ₮ · ₩ · ¥ · zł · ₴ · Q · ₪ · TL . en.wikipedia.org/wiki/Currency_signBartolomeo
What to get even dirtier? Use the list of valid currency symbols. If it's one of them use it. Otherwise strip all a-Z characters.Silvers
In my particular case, as I mentioned above, it's much more convenient to create a list "CODE -> LOCAL_SYMBOL". It seems, that I'll have to solve my problem this way.Bartolomeo
no worries :) glad you found a solution. did you report it as a bug to Apple?Silvers
No, I didn't. Will do. Thanks :)Bartolomeo

© 2022 - 2024 — McMap. All rights reserved.