-1

I am trying to format a currency value like this:

let price: Double = 1
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.currencyCode = "USD"
formatter.locale = Locale(identifier: "en_US")
let priceString = formatter.string(from: NSNumber(value: Double(price / 100)))

But the result I get is "US$ 0,01" instead of "$ 0,01".How can I remove the 'US' extra suffix in front of it?

Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187
  • Does this answer your question? [iOS Currency Format - remove the text before the symbol](https://stackoverflow.com/questions/33653624/ios-currency-format-remove-the-text-before-the-symbol) – Larme Jun 07 '22 at 09:44
  • 1
    For people wanting to test it into Playground, the default locale is `en_US`, so you can't replicate it. But if you set the `formatter.local = Local(identifier: "fr_FR")` for instance, you'll see it, or by changing the `currencyCode` to not `USD` (since it's the one of the locale) – Larme Jun 07 '22 at 09:48
  • Please check my answer where I create a Playground file to play with currency: https://stackoverflow.com/a/72239962/6808357 – André Henrique da Silva Jun 07 '22 at 10:04
  • Even changing the locale to "en_US" doesn't solve the problem. I updated the question @Larme – Ramy Al Zuhouri Jun 07 '22 at 10:34
  • 1
    That's strange. I tested in Playground: with `fr_FR`, I got `0,01 $US`, with `en_US`, I got `$0.01`, with `en_UK`, I got `US$0.01`. – Larme Jun 07 '22 at 10:37
  • 2
    You should not get a comma as decimal separator in your output if the locale is set to en_US. Is that really the code that produces "US$ 0,01"? – Joakim Danielson Jun 07 '22 at 11:03

1 Answers1

1

NumberFormatter is old API. Below is the modern syntax.


You need to find a locale in which "US" is implicit but where people use commas instead of periods, as we do in the US.

0.01.formatted(.currency(code: "USD"))

yields "$0.01" in Locale(identifier: "en_US").

0.01.formatted(
  FloatingPointFormatStyle.Currency(code: "USD", locale: .init(identifier: "de_DE"))
)

yields "0,01 $".