iPhone: How to get local currency symbol (i.e. “$”

2019-01-08 09:08发布

问题:

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.

回答1:

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)



回答2:

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.



回答3:

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.



回答4:

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.



回答5:

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.



回答6:

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".



回答7:

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?