【问题标题】:how to format number to currency by currency name in JavaScript/Angular如何在 JavaScript/Angular 中按货币名称将数字格式化为货币
【发布时间】:2020-06-16 04:57:22
【问题描述】:

如何通过货币名称将数字格式化为货币?

我有只有货币名称的动态数字(例如:美元、印度卢比等)。我没有任何本地代码(例如 en-US)。

我会尝试以下方式。但它要求提供本地代码('en-US')。

var formatter = new Intl.NumberFormat('en-US', {
  style: 'currency',
  currency: 'USD',
});

formatter.format(2500)

但我的数据库中没有 'en-US'。我只有货币名称和数字。

那么我怎样才能通过货币名称将数字格式化为货币?

【问题讨论】:

    标签: javascript angular typescript currency


    【解决方案1】:

    只需通过 undefined 获取语言环境?

    const getFormattedCurrency = (currency, amount) => new Intl.NumberFormat(undefined, {
      style: 'currency',
      currency,
    }).format(amount);
    
    console.log(getFormattedCurrency('USD', 2500));
    console.log(getFormattedCurrency('JPY', 2500));
    console.log(getFormattedCurrency('EUR', 2500));
    console.log(getFormattedCurrency('CNY', 2500));
    console.log(getFormattedCurrency('AUD', 2500));
    console.log(getFormattedCurrency('INR', 2500));

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-18
      • 1970-01-01
      • 2018-03-06
      • 1970-01-01
      • 1970-01-01
      • 2014-12-31
      • 2016-10-01
      • 1970-01-01
      相关资源
      最近更新 更多