number.toLocaleString()显示某些语言的货币符号,显示其他语言的代码

时间:2020-06-22 11:29:17

标签: javascript

我使用的是number.toLocaleString()函数,我的问题是,对于某些货币,它输出带有货币符号的价格,而对于其他货币,则输出货币代码。

有些货币没有符号,但是全部都有代码。我想对此进行标准化,并显示所有货币的代码。

当前代码:

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

当前输出(如果国家为美国,货币为USD:$1.00 预期输出:USD 1.00

当前和预期的产出(如果国家CZ,货币CZK:CZK 26.10

2 个答案:

答案 0 :(得分:2)

添加“货币显示”作为选项并将其设置为“代码”。

currencyDisplay:“代码”

答案 1 :(得分:2)

使用选项currencyDisplay: 'code'

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