2

How i can get currency symbol by currency code when device in any Locale?

I try use something like this:

- (NSLocale *)priv_findLocaleByCurrencyCode:(NSString *)currencyCode
{
    NSArray *locales = [NSLocale availableLocaleIdentifiers];
    NSLocale *locale = nil;

    for (NSString *localeId in locales) {
        locale = [[NSLocale alloc] initWithLocaleIdentifier:localeId];
        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 priv_findLocaleByCurrencyCode:currencyCode];
    NSString *currencySymbol;
    if (locale)
        [fmtr setLocale:locale];
    [fmtr setNumberStyle:NSNumberFormatterCurrencyStyle];
    currencySymbol = [fmtr currencySymbol];

    return currencySymbol;
}

Example:

[self findCurrencySymbolByCode:@"PLN"];

But if i in Russian locale, and try to find symbol for currency code PLN i get - PLN, instead of "zl" symbol. Because NSLocale have two locale for PL - en_PL and pl_PL, and i did't know how to check not wrong.

May be some another path to make this?

Nubaslon
  • 741
  • 2
  • 11
  • 27

1 Answers1

0

I think your currency code or symbol is different than you think.. Refer this link

Replace the following line

locale = [[NSLocale alloc] initWithLocaleIdentifier:localeId];

with

locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_PL"];

Check with above code.

Balaji Kondalrayal
  • 1,743
  • 3
  • 19
  • 38
  • @Nubaslon - then your currency is Polish Zloty – Balaji Kondalrayal Nov 17 '15 at 12:25
  • Yes, but curency symbol is - "PLN" instead of "zl", i need "zl" – Nubaslon Nov 17 '15 at 12:26
  • Updated the answer.. Check with that and let me know if not working and reply with ios version you are working with. – Balaji Kondalrayal Nov 17 '15 at 12:46
  • If i use - "en_PL" i get symbol "PLN", if use "pl_PL" - i get "zl". But how i can find right locale? iOS 9.1 – Nubaslon Nov 17 '15 at 12:47
  • Okie.. I got it.. Your preferred language in your device is english. So its always en_PL as locale identifier. If you change your preferred language to your required locale then it will take locale identifier as pl_PL and will also show the zl symbol. – Balaji Kondalrayal Nov 17 '15 at 13:04
  • Yes, may be, but how i can get right symbol, if i in random device language and use currency for using in app? i need right symbol regardless of locale and device language( – Nubaslon Nov 17 '15 at 13:10
  • @Nubaslon - No actually you cant. @Balaji Kondalrayal is correct. Because in your locale only people know about the symbol of currency, so user in INDIA or USA may not know about your currency symbol. So they know about the currency name. You cant always work with same locale. So check with your preferred language and change the `identifier string` with some alterations and go. – shlok Nov 17 '15 at 13:16
  • Check this link http://stackoverflow.com/questions/3910244/getting-current-device-language-in-ios. It might help you to understand – Balaji Kondalrayal Nov 17 '15 at 13:32