1

Is there a way to set the NSLocale for a NSNumberFormatter via the currency code instead of the locale identifier in iOS?

Like, for instance, to use "USD" instead of "en_US" in any point of the locale setting?

I see there's a NSLocaleCurrencyCode object, but I'm not sure what method I can use from it to set the NSNumberFormatter's locale.

I'm currently using Swift, if that makes the process any easier.

EDIT: For further explanation of what I'm doing, I have a list of every single country's currency code (USD, GBP, JPY, CNY, etc...). The user picks whichever currency he's using via these codes, and then I want the NSNumberFormatter to automatically set its locale to the locale of that currency to automatically format it.

Gabriel Garrett
  • 2,087
  • 6
  • 27
  • 45
  • Keep in mind that `USD` is used in multiple locales. The Euro is used in many locales. Better explain what you have and what you are trying to accomplish. – rmaddy Oct 27 '14 at 17:19
  • So I have a list of every single country's currency code (USD, GBP, JPY, CNY, etc...). The user picks whichever currency he's using via these codes, and then I want the NSNumberFormatter to automatically set its locale to the locale of that currency to automatically format it. – Gabriel Garrett Oct 27 '14 at 17:57
  • That's still unclear. Do you have a list of countries and its associated currency code or do you just have a list of currency codes? Like I said, codes like EUR are used with many different countries. There is no specific locale tied to a given currency code. Currency should be formatted based on the user's locale, not any specific currency code. – rmaddy Oct 27 '14 at 18:01
  • It's a list of currency codes. The user doesn't select the country. It's for when a user is traveling abroad and wants to enter the price of a foreign currency. I want their input formatted in that currency when they select it. – Gabriel Garrett Oct 27 '14 at 18:03

1 Answers1

6

You cannot determine a specific locale from currency code.

For example, the number of locales which have currency code USD is 27, and NSNumberFormatter with these NSLocale emits formatted string with 7 variations:

"$123,456.78":    [en_PR, es_US, en_MH, chr_US, en_AS, en_MP, es_SV, en_VG, en_VI, es_PR, haw_US, en_IO, en_UM, en_US, en_DG, en_GU]
"US$123,456.78":  [en_PW, sn_ZW, en_ZW, en_TC, nd_ZW, en_FM]
"$123.456,78":    [es_EC]
"123 456,78 US$": [pt_TL]
"$ 123456.78":    [en_US_POSIX]
"$ 123.456,78":   [nl_BQ]
"$ 123,456.78":   [lkt_US]

If you want do what you want, you should maintain a lookup table by yourself.

let currency2locale = [
    "USD": NSLocale(localeIdentifier: "en_US"),
    "GBP": NSLocale(localeIdentifier: "en_GB"),
    "JPY": NSLocale(localeIdentifier: "ja_JP"),
    ...
]
rintaro
  • 51,423
  • 14
  • 131
  • 139
  • Thank you, I figured I would have to do something like this. I'll probably set an if statement so that if the user picks any of the 27, it's "en_US". However, I still want to do currency conversions, so I will still need to write an if statement for each currency. Anyway, thanks again! – Gabriel Garrett Oct 28 '14 at 12:38
  • Hey rintaro, just wondering, where did you get the information that said that the number of locales associated with the currency code USD is 27? – Gabriel Garrett Mar 18 '15 at 22:09
  • 3
    Just counted :). `(NSLocale.availableLocaleIdentifiers() as [String]).filter({NSLocale(localeIdentifier: $0).objectForKey(NSLocaleCurrencyCode) as? String == "USD"}).count` – rintaro Mar 19 '15 at 03:24