Home > Net >  don't get currency symbol from NumberFormatter
don't get currency symbol from NumberFormatter

Time:06-26

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")

CodePudding user response:

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")
    

    CodePudding user response:

    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

    • Related