don't get currency symbol from NumberFormatter

628 Views Asked by At

The output of following Code is: ARS 59.00. Why is there no symbol printed?

let formatter = NumberFormatter()
formatter.currencyCode = "ARS"
formatter.numberStyle = .currency
print(formatter.string(from: NSNumber(value: 59.00)) ?? "na")
2

There are 2 best solutions below

1
Kuvonchbek Yakubov On BEST ANSWER

Forcing a Custom Locale

To display currency, you will need to show the currency symbol ($, €, ¥, £) for the current locale.

NumberFormatter will show the correct symbol, and the formatting that you might not realize is very different from what you're used to. Different countries use different decimal separators and grouping separators—take a look!

  • In the USA: $3,490,000.89
  • In France: 3 490 000,89 €
  • In Germany: 3.490.000,89 €
  • See: How to Use NumberFormatter (NSNumberFormatter) in Swift to Make Currency Numbers Easy to Read

    So in your case it would be:

    let formatter = NumberFormatter()
    formatter.currencyCode = "ARS"
    formatter.numberStyle = .currency
    formatter.locale = Locale(identifier: "es_AR")
    print(formatter.string(from: NSNumber(value: 59.00)) ?? "na")
    
    1
    user3305074 On

    Try this:

    let formatter = NumberFormatter()
    formatter.numberStyle = .currency
    formatter.locale = Locale(identifier: "es_AR")
    print(formatter.string(from: NSNumber(value: 59.00)) ?? "na")
    

    check the local identifier here: https://gist.github.com/jacobbubu/1836273