【问题标题】:Browser intl.NumberFormat not displaying currency symbols correctly浏览器 intl.NumberFormat 未正确显示货币符号
【发布时间】:2016-05-12 18:51:28
【问题描述】:

我正在尝试使用 Intl.NumberFormat 编写货币格式化函数。 当我将美元或欧元等货币作为货币传递时,它可以正常工作,但当我传递更模糊的货币代码(如 PLN 或 COL)时,它似乎会中断,而不是按要求显示它们的符号,而是显示代码。它可以清楚地识别代码,因为当我要求它显示名称时,它可以正常工作:

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

同时显示“PLN43”

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

显示“43.00 波兰兹罗提”

【问题讨论】:

  • 没有权限先检查但是如果你在上面的例子中指定 pl-PL 作为第一个参数会发生什么?

标签: javascript typescript ecmascript-6 ecma


【解决方案1】:

Intl.NumberFormat 应该有你需要的符号,你只需要确保你指定了正确的语言代码。

您可以在此处找到 ISO 语言代码的映射: https://www.w3schools.com/tags/ref_language_codes.asp

在这种情况下,您需要使用波兰语值“pl”而不是“en-US”

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

【讨论】:

  • 所以这意味着我们必须更改数字格式才能使用符号。如果我们想用符号显示多个“晦涩”的货币,格式将不一致...[['pl', 'PLN'], ['th', 'THB']].map(([locale, currency]) => new Intl.NumberFormat(locale, { style: 'currency', currency, currencyDisplay: 'symbol' }).format(10000)) 产生["10 000,00 zł", "฿10,000.00"]
【解决方案2】:

根据spec

但是,可以使用本地化货币符号的货币代码和语言标签组合的集合取决于实现。如果本地化货币符号不可用,则使用 ISO 4217 货币代码进行格式化。

【讨论】:

  • “不可用”是什么意思...?有没有什么地方可以为这些 ISO 代码提供货币符号,以便我可以继续使用这个格式化程序,还是我必须想出一个替代解决方案?
  • 我建议可以使用symbols that are present in unicode
猜你喜欢
  • 2019-02-23
  • 1970-01-01
  • 1970-01-01
  • 2021-07-30
  • 2018-02-11
  • 2015-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多