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

2019-01-08 08:58发布

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.

7条回答
欢心
2楼-- · 2019-01-08 09: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.

查看更多
对你真心纯属浪费
3楼-- · 2019-01-08 09:16

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.

查看更多
闹够了就滚
4楼-- · 2019-01-08 09:19

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

查看更多
Ridiculous、
5楼-- · 2019-01-08 09:22

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?

查看更多
beautiful°
6楼-- · 2019-01-08 09:23
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.

查看更多
做个烂人
7楼-- · 2019-01-08 09:29

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)

查看更多
登录 后发表回答