如何在Javascript中使用尾随ISO代码格式化货币?

如何在Javascript中使用尾随ISO代码格式化货币?,javascript,format,Javascript,Format,给定值1000.5,我想将其格式化为1000.50美元,其中尾随货币代码是最相关的部分。性能也很重要,因为格式将针对数百万个值发布。因此,不鼓励使用正则表达式 您可以使用JavaScript或其他一些标准化的JavaScript数字格式化API来实现这一点吗 例如: 实际: 1000.50美元 预期: 1000.50美元 这里有一个简单的函数,它使用一个尾随的ISO代码进行格式化 function FormatWithTrailingISO(currency, type,num) {

给定值1000.5,我想将其格式化为1000.50美元,其中尾随货币代码是最相关的部分。性能也很重要,因为格式将针对数百万个值发布。因此,不鼓励使用正则表达式

您可以使用JavaScript或其他一些标准化的JavaScript数字格式化API来实现这一点吗

例如:

实际:

1000.50美元

预期:

1000.50美元


这里有一个简单的函数,它使用一个尾随的ISO代码进行格式化

   function FormatWithTrailingISO(currency, type,num) {
    const formatter = Intl.NumberFormat(type, {
      style: "currency",
      currency: currency,
      currencyDisplay: "code",
    });
    return formatter.format(num).replace(currency, "") + " " + currency;
   }
用法:

console.log(FormatWithTrailingISO("USD", "en-US", 1000.5).trim());

这里有一个简单的函数,它使用一个尾随的ISO代码进行格式化

   function FormatWithTrailingISO(currency, type,num) {
    const formatter = Intl.NumberFormat(type, {
      style: "currency",
      currency: currency,
      currencyDisplay: "code",
    });
    return formatter.format(num).replace(currency, "") + " " + currency;
   }
用法:

console.log(FormatWithTrailingISO("USD", "en-US", 1000.5).trim());