Filip
Filip

Reputation: 945

number.toLocaleString() displays currency symbol for some languages, code for other

I'm using a number.toLocaleString() function, my problem is that, for some currencies, it outputs the price with a currency symbol, in other it outputs the currency codes.

Some of the currencies don't have a symbol, but all have a code. I want to normalize this and display the code for all currencies.

Current code:

  const convertedPrice = Math.round(convertPrice(price, currency) * 10) / 10;
  const formattedPrice = convertedPrice.toLocaleString(country, {
    style: "currency",
    currency: currency
  });

Current output if country US, currency USD: $1.00 Expected output: USD 1.00

Current and expected output if country CZ, currency CZK: CZK 26.10

Upvotes: 0

Views: 953

Answers (2)

Clyde The Cloud
Clyde The Cloud

Reputation: 46

Use option currencyDisplay: 'code'.

const formattedPrice = convertedPrice.toLocaleString(country, {
    style: "currency",
    currency: "currency",
    currencyDisplay: "code"
  });

Upvotes: 1

user13782377
user13782377

Reputation:

Add "currency Display" as an option and set it to "code".

currencyDisplay:"code"

Upvotes: 1

Related Questions