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
3 Answers
Reset to default 3The 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
版权声明:本文标题:javascript - Browser intl.NumberFormat not displaying currency symbols correctly - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741935109a2405807.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论