I have get currency code (Eg: USD, EUR, INR) from webservice response. I need to show the currency symbols for the corresponding currency code. If the currency code is USD, i need to show $, if the currency code is EUR i need to show €. How can i do this? Please suggest any idea or sample code to do this. Please help me. Thanks in advance.
问题:
回答1:
This code works charm in my project. I will share this to you all.
NSString *currencyCode = @"EUR";
NSLocale *locale = [[[NSLocale alloc] initWithLocaleIdentifier:currencyCode] autorelease];
NSString *currencySymbol = [NSString stringWithFormat:@"%@",[locale displayNameForKey:NSLocaleCurrencySymbol value:currencyCode]];
NSLog(@"Currency Symbol : %@", currencySymbol);
Thanks.
回答2:
This code is what you are looking for though not very efficient because of loop for locales. Still it works correctly for all currency codes, not just for eur or usd. Hope this will help you.
- (NSLocale *) findLocaleByCurrencyCode:(NSString *)_currencyCode
{
NSArray *locales = [NSLocale availableLocaleIdentifiers];
NSLocale *locale = nil;
for (NSString *localeId in locales) {
locale = [[[NSLocale alloc] initWithLocaleIdentifier:localeId] autorelease];
NSString *code = [locale objectForKey:NSLocaleCurrencyCode];
if ([code isEqualToString:_currencyCode])
break;
else
locale = nil;
}
return locale;
}
- (NSString *)findCurrencySymbolByCode:(NSString *)_currencyCode
{
NSNumberFormatter *fmtr = [[NSNumberFormatter alloc] init];
NSLocale *locale = [self findLocaleByCurrencyCode:_currencyCode];
NSString *currencySymbol;
if (locale)
[fmtr setLocale:locale];
[fmtr setNumberStyle:NSNumberFormatterCurrencyStyle];
currencySymbol = [fmtr currencySymbol];
[fmtr release];
if (currencySymbol.length > 1)
currencySymbol = [currencySymbol substringToIndex:1];
return currencySymbol;
}
Use it this way:
NSString *currencySymbol = [self findCurrencySymbolByCode:currencyCode];
回答3:
Swift 4 version
Finding locale by currency code:
let localeGBP = Locale
.availableIdentifiers
.lazy
.map { Locale(identifier: $0) }
.first { $0.currencyCode == "GBP" }
print(localeGBP?.currencySymbol) // £
Formatting currency
if let locale = localeGBP {
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.locale = locale
let result = formatter.string(from: 100000) // £100,000.00
}
Edit:
Why .lazy
? Without it the loop would run over all the locale identifiers and return the first one which matches. That's about 700ish identifiers, and if the first one is the one you want then you have wasted creating 699 Locales :) With .lazy
in there it automatically stops at the first matching one. In my case it reduces the number of times through the loop from 710 down to 22 when converting "GBP". This isn't important if you are only doing this once, but if you're doing this a number of times (i.e. over an array of symbols) then it's an easy way to get a bit more efficiency.
回答4:
NSLocale
will happily tell you the currency symbol used by a particular locale:
[locale objectForKey:NSLocaleCurrencySymbol];
It'll also tell you the currency code:
[locale objectForKey:NSLocaleCurrencyCode];
So all you have to do now is look up the locale that corresponds to a given code. There's no built-in method (that I'm aware of) to do this directly, so loop through all the known locales and pick the one that matches. @Umka's answer has a good example of this in the -findLocaleByCurrencyCode:
method.
You could optimise the process by building your own lookup table, rather than iterating through all locales each time. You may need to handle the possibility of duplicate currency codes too, which would require some heuristic for deciding which is the most likely locale.
回答5:
Building on code from @Mike Abdullah and @Umka, here are some functions in Swift.
func findCodeAndSymbolForAllLocales() {
let locales = NSLocale.availableLocaleIdentifiers()
for localeId in locales {
let locale = NSLocale(localeIdentifier: localeId)
if let code = locale.objectForKey(NSLocaleCurrencyCode) as? String,
let symbol = locale.objectForKey(NSLocaleCurrencySymbol) {
print("\(code) \(symbol)")
}
}
}
func findCurrencySymbolByCode(currencyCode:String) -> String? {
guard let locale = findLocaleByCurrencyCode(currencyCode) else {
print("locale for \(currencyCode) is nil")
return nil
}
return locale.objectForKey(NSLocaleCurrencySymbol) as? String
}
func findLocaleByCurrencyCode(currencyCode:String) -> NSLocale? {
let locales = NSLocale.availableLocaleIdentifiers()
var locale:NSLocale?
for localeId in locales {
locale = NSLocale(localeIdentifier: localeId)
if let code = locale!.objectForKey(NSLocaleCurrencyCode) as? String {
if code == currencyCode {
return locale
}
}
}
return locale
}
回答6:
NSNumberFormatter * formatter = [NSNumberFormatter new];
formatter.numberStyle = NSNumberFormatterCurrencyStyle;
NSString * localeIde = [NSLocale localeIdentifierFromComponents:@{NSLocaleCurrencyCode: currencyCode}];
formatter.locale = [NSLocale localeWithLocaleIdentifier:localeIde];
NSString * symbol = formatter.currencySymbol;
回答7:
One way is to start with a valid locale (e.g. the user current locale) and then override the currency code. Simple and efficient and allows you to use the newly constructed locale to configure a currency formatter in order to display a string according to the user's locale preferences:
NSLocale* locale = [NSLocale currentLocale];
if (_currencyCode) {
NSMutableDictionary* components = [[NSLocale componentsFromLocaleIdentifier:locale.localeIdentifier] mutableCopy];
components[NSLocaleCurrencyCode] = _currencyCode;
locale = [[NSLocale alloc] initWithLocaleIdentifier:[NSLocale localeIdentifierFromComponents:components]];
}
return locale.currencySymbol;
回答8:
NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
this is for US currency style
NSNumberFormatter *numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
//[numberFormatter setCurrencySymbol:@"Rs"];
[numberFormatter setNumberStyle:@"en_IN"];
This is for indian
回答9:
Swift
let locale = NSLocale(localeIdentifier: currencyCode)
let currencySymbol = locale.displayName(forKey: .currencySymbol, value: currencyCode) ?? currencyCode
print("Currency symbol: \(currencySymbol)")