IPhone: how to get the local currency symbol (ie. "$" Do not use "AU $")

Here is the code for how I now get the currency symbol:

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 the Apple development documentation, for each currency there are at least two currency symbols (these symbols may be the same, though) - local (for example, used in the country. $ For Australia) and international (AU $ for Australia). So, the question is how to get the LOCAL currency symbol. Any ideas?

Thanks in advance.

+26
formatting objective-c iphone currency
Dec 13 '10 at
source share
6 answers

Your code should work, however the wrong locale id. It must be "en_AU".

See “Using a Local 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 )

+9
Nov 25 '11 at 4:27
source share
 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 the USA, ¥ 5.00 for Japan, 5.00 euros for Europe, etc.

+18
Jun 16 2018-12-12T00:
source share

This fragment returns the currency symbol ¥ for the locale "ja_JP" (it can 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 a local currency symbol instead of $, but this should be the way it should be displayed on iOS. However, note that the international character printed for au_AU is not AU $, but AUD.

+12
Dec 15 2018-10-15
source share

It is not ideal in that it does not log out, but obviously you can create your own internal table using a list of current currency symbols * . Since there are unicode characters in this list, you just need to map the list of Apple locales to the list for it.

You know, just in case provided by Apple are not available.

* Note : the link is not intended for authorization, see comments.

+9
Dec 15 2018-10-15
source share

You can, if you need to, create a .strings file that contains a currency and use the NSLocalizedString function to create a localized currency. Something like that:

 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 it like this:

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

However, this means that for each supported localization you need a .strings file. In addition, this means that for some localizations the currency symbol will not be enough to display the proper money format, you will need to use something like this:

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

This has some limitations, but it may work for you.

0
Dec 17 2018-10-17
source share

Could you get the current line and remove it from all aZ characters? If the resulting string has a length> 0, you will get your character. Otherwise, use the original string.

It's sticky, and I'm not sure if this will work for all currencies around the world, but can it work?

0
Dec 20 '10 at 4:55
source share



All Articles