简体   繁体   English

如何在 JavaScript/Angular 中按货币名称将数字格式化为货币

[英]how to format number to currency by currency name in JavaScript/Angular

How to format number to currency by currency name?如何按货币名称将数字格式化为货币?

I have dynamic numbers with only currency name ( ex: USD,INR etc).我有只有货币名称的动态数字(例如:美元、印度卢比等)。 I don't have any local codes (like, en-US).我没有任何本地代码(例如 en-US)。

I would try this below way.我会尝试以下方式。 But it is asking to provide local code('en-US').但它要求提供本地代码('en-US')。

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

formatter.format(2500)

But I don't have 'en-US' in my data base.但我的数据库中没有“en-US” I have only currency Name and Number.我只有货币名称和数字。

So how could I format the the number to currency by Currency name?那么如何通过货币名称将数字格式化为货币?

Just pass undefined for the locale?只需为语言环境传递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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM