Use Intl or the number prototype to format number to any specified currency.
const money = 420000;
const moneyFormatted = new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
}).format(money);
console.log(moneyFormatted); // $420,000.00
- Javascript falls back on runtime locale if you specify an unavailable locale. This is used to format number
- You can see the complete list of currency codes here.
Or, use number prototype-
const money = 420000;
const moneyFormatted = money.toLocaleString("en-IN", {
maximumFractionDigits: 2,
style: "currency",
currency: "INR",
});
console.log(moneyFormatted); // ₹ 4,20,000.00
If you are using a framework that does not provide direct currency support just include the above in a globally accessible script and use it everywhere. For example, you would define the format in global filters in Vue and use that filter wherever needed.