开发者问题收集

Intl.NumberFormat 抛出错误“未捕获的 RangeError:无效的货币代码”

2020-09-27
6936

我想编写一个格式函数来显示欧元价格,但它会抛出错误。哪里出了问题?

const formatVal = val => new Intl.NumberFormat("de-DE", {
  style: "currency",
  currency: "€"
}).format(val)

formatVal(456)

抛出的错误:

Uncaught RangeError: Invalid currency code : â¬

2个回答

将 € 更改为 EUR

const formatVal = val => new Intl.NumberFormat("de-DE", {
  style: "currency",
  currency: "EUR"
}).format(val)

console.log(formatVal(456))
Naor Tedgi
2020-09-27

来自 MDN

The currency to use in currency formatting. Possible values are the ISO 4217 currency codes, such as " USD " for the US dollar, " EUR " for the euro, or " CNY " for the Chinese RMB — see the Current currency & funds code list . There is no default value; if the style is " currency ", the currency property must be provided.

不是有效的 ISO 4217 货币代码。请改用 EUR

Ivar
2020-09-27