How can i get currency symbols from currency code

2020-02-20 07:26发布

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.

9条回答
男人必须洒脱
2楼-- · 2020-02-20 07:55
NSNumberFormatter * formatter = [NSNumberFormatter new];
formatter.numberStyle = NSNumberFormatterCurrencyStyle;

NSString * localeIde = [NSLocale localeIdentifierFromComponents:@{NSLocaleCurrencyCode: currencyCode}];
formatter.locale = [NSLocale localeWithLocaleIdentifier:localeIde];

NSString * symbol = formatter.currencySymbol;
查看更多
Fickle 薄情
3楼-- · 2020-02-20 07:55

Swift

let locale = NSLocale(localeIdentifier: currencyCode)
let currencySymbol = locale.displayName(forKey: .currencySymbol, value: currencyCode) ?? currencyCode
print("Currency symbol: \(currencySymbol)")
查看更多
姐就是有狂的资本
4楼-- · 2020-02-20 08:00

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.

查看更多
一夜七次
5楼-- · 2020-02-20 08:00

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;
查看更多
狗以群分
6楼-- · 2020-02-20 08:01

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.

查看更多
Emotional °昔
7楼-- · 2020-02-20 08:07
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

查看更多
登录 后发表回答