1

how to set NumberFormatter to return currency symbol not the iso one but local which is visible in iOS locale settings pane. Like for Polish currency I need to have “zł” symbol not the “PLN”.

I cannot find any way to get it from system as this cannot be hardcoded. IAP localized price also uses “zł” not “PLN”

I tried this way:

let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.currencySymbol = Locale.current.currencySymbol
formatter.maximumFractionDigits = 2

let price = formatter.string(from: (offeringPrice / 12) as NSNumber) ?? ""

but whatever I try to use as currency symbol I always get in return "PLN"

Paweł Madej
  • 1,229
  • 23
  • 42
  • `Locale` can have 2 components, the language and the region. The `currencySymbol` can vary depending on the region and language independently. If I don't set the region, but just the language: `Locale(identifier: "pl").currencySymbol` it returns `"¤"`, but if I set both the region and language: `Locale(identifier: "pl-PL").currencySymbol` It returns `"zł"`. I'm wondering if you are getting unexpected results due to the region not being set correctly on your test device. – Ryan Maloney May 28 '20 at 18:50
  • I have locale Poland and region Poland set in device, localised IAP price returns "zł" so this is bit weird – Paweł Madej May 28 '20 at 18:57
  • my app language is English only for now. maybe this is cause? and locale en_PL for whatever reason ;/ – Paweł Madej May 28 '20 at 19:00

2 Answers2

2

If you set formatter.locale to pl_PL it works:

let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.maximumFractionDigits = 2
formatter.currencyCode = "PLN"

let polandLocale = Locale(identifier: "pl_PL")
let usLocale = Locale(identifier: "en_US")

let offeringPrice = 50.0
let price = (offeringPrice / 12) as NSNumber

formatter.locale = usLocale
formatter.string(from: price) // "PLN 4.17"

formatter.locale = polandLocale
formatter.string(from: price) // "4,17 zł"
jdaz
  • 5,964
  • 2
  • 22
  • 34
-1

I've found this solution

    func getCurrencySymbol(from currencyCode: String) -> String? {
        let locale = NSLocale(localeIdentifier: currencyCode)
        if locale.displayName(forKey: .currencySymbol, value: currencyCode) == currencyCode {
            let newlocale = NSLocale(localeIdentifier: currencyCode.dropLast() + "_en")
            return newlocale.displayName(forKey: .currencySymbol, value: currencyCode)
        }
        return locale.displayName(forKey: .currencySymbol, value: currencyCode)
    }

original answer here: https://stackoverflow.com/a/49146857/1285959

Paweł Madej
  • 1,229
  • 23
  • 42