Home > Back-end >  Formatter currency value returns 'US' extra suffix
Formatter currency value returns 'US' extra suffix

Time:06-09

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?

CodePudding user response:

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 $".

  • Related