admin管理员组

文章数量:1313599

I'm attempting to write a currency formatting function using Intl.NumberFormat. It works correctly when I pass it things like USD, or EUR as the currency, but seems to break when I pass it more obscure currency codes like PLN or COL, and instead of displaying their symbols as requested it displays the Codes. It is clearly recognizing the code because when I ask it to display the name instead it works correctly:

Intl.NumberFormat("en-US",{
  style:'currency',
  minimumIntegerDigits:1,
  currency: 'PLN',
  currencyDisplay: 'symbol'
}).format(43);

Displays "PLN43" while

Intl.NumberFormat("en-US",{
  style:'currency',
  minimumIntegerDigits:1,
  currency: 'PLN',
  currencyDisplay: 'name'
}).format(43);

Displays "43.00 Polish zlotys"

I'm attempting to write a currency formatting function using Intl.NumberFormat. It works correctly when I pass it things like USD, or EUR as the currency, but seems to break when I pass it more obscure currency codes like PLN or COL, and instead of displaying their symbols as requested it displays the Codes. It is clearly recognizing the code because when I ask it to display the name instead it works correctly:

Intl.NumberFormat("en-US",{
  style:'currency',
  minimumIntegerDigits:1,
  currency: 'PLN',
  currencyDisplay: 'symbol'
}).format(43);

Displays "PLN43" while

Intl.NumberFormat("en-US",{
  style:'currency',
  minimumIntegerDigits:1,
  currency: 'PLN',
  currencyDisplay: 'name'
}).format(43);

Displays "43.00 Polish zlotys"

Share Improve this question asked May 12, 2016 at 18:51 Gabe O'LearyGabe O'Leary 2,0705 gold badges25 silver badges42 bronze badges 1
  • 1 No access to check first but what happens if you specify pl-PL as the first parameter in the examples above? – user1645942 Commented Jul 25, 2017 at 22:31
Add a ment  | 

3 Answers 3

Reset to default 3

The Intl.NumberFormat should have the symbols you need, you just have to make sure you specify the correct language code.

You can find a mapping of ISO language codes here: https://www.w3schools./tags/ref_language_codes.asp

In this case you will need to use the Polish value "pl" instead of "en-US"

Intl.NumberFormat("pl",{
  style:'currency',
  minimumIntegerDigits:1,
  currency: 'PLN',
  currencyDisplay: 'symbol'
}).format(43);

To display just the currency symbol more concisely, you might consider using currencyDisplay: 'narrowSymbol' instead of symbol in your Intl.NumberFormat configuration. This option is also applicable for toLocaleString. As explained in the MDN documentation, narrowSymbol provides a more streamlined symbol format, e.g., "$100" rather than "US$100".

Intl.NumberFormat("en-US", {
  style: 'currency',
  currency: 'PLN',
  currencyDisplay: 'narrowSymbol'
}).format(43);

Displays 'zł 43.00'

However, please verify the patibility with your target browsers here on CanIUse as narrowSymbol is not supported in some older or less mon browsers.

According to the spec:

However, the set of binations of currency code and language tag for which localized currency symbols are available is implementation dependent. Where a localized currency symbol is not available, the ISO 4217 currency code is used for formatting.

本文标签: javascriptBrowser intlNumberFormat not displaying currency symbols correctlyStack Overflow