简体   繁体   中英

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).

I would try this below way. But it is asking to provide local code('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. 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?

 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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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