Reputation: 38683
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?
Upvotes: 0
Views: 252
Reputation: 20699
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));
Upvotes: 2