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.

Yossi
  • 2,525
  • 2
  • 21
  • 24
kovpas
  • 9,553
  • 6
  • 40
  • 44
  • 1
    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. – Thomas Tempelmann Dec 19 '10 at 22:34
  • Yep - definitely a bug. I tried this and en_US returns $ – mackross Dec 20 '10 at 06:26
  • @Thomas Tempelmann: I've just filed a bug. Thanks. – kovpas Dec 22 '10 at 08:38

7 Answers7

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.

dgund
  • 3,459
  • 4
  • 39
  • 64
  • The question was about specific locales, not "[NSLocale currentLocale]". Anyways, Nik provided correct answer a couple of months ago. Thanks. – kovpas Jun 18 '12 at 08:35
  • 2
    ¥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. – gnasher729 Sep 11 '15 at 16:15
  • 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];` – Ahsan Ebrahim Khatri Mar 24 '16 at 07:40
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.

Julio Gorgé
  • 10,056
  • 2
  • 45
  • 60
  • 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. – kovpas Dec 15 '10 at 20:20
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)

Nik
  • 5,801
  • 2
  • 31
  • 23
  • Holy c... Thank you man :). I solved this problem in other way, but this really works, I've just checked :). – kovpas Nov 25 '11 at 11:03
  • `NSString *currencySymbol = [[NSLocale currentLocale] objectForKey:NSLocaleCurrencySymbol];` – CyberMew Jan 09 '17 at 07:27
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.

Matthew Frederick
  • 22,245
  • 10
  • 71
  • 97
  • 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. – kovpas Dec 15 '10 at 20:21
  • 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. – Thomas Tempelmann Dec 19 '10 at 22:32
  • 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. – Matthew Frederick Dec 19 '10 at 22:45
  • 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. – v1Axvw Dec 21 '10 at 18:10
  • 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. – Matthew Frederick Dec 21 '10 at 20:04
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".

neowinston
  • 7,584
  • 10
  • 52
  • 83
Alex
  • 1,574
  • 17
  • 36
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.

Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
  • 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. – kovpas Dec 17 '10 at 17:53
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?

mackross
  • 2,234
  • 17
  • 19
  • 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 . http://en.wikipedia.org/wiki/Currency_sign – kovpas Dec 20 '10 at 06:12
  • 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. – mackross Dec 20 '10 at 06:29
  • 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. – kovpas Dec 20 '10 at 08:10
  • no worries :) glad you found a solution. did you report it as a bug to Apple? – mackross Dec 20 '10 at 08:12
  • No, I didn't. Will do. Thanks :) – kovpas Dec 22 '10 at 08:23