2 Ways to Format Currency in Javascript

Last updated on September 7, 2022 Snowball Loading... Post a comment

In Javascript, you can format currency strings in 2 different ways:

  • Intl.NumberFormat()
  • Number.toLocaleString()

Both of these approaches work on all modern web browsers and Node.js No third-party libraries are required.

Example of using Intl.NumberFormat:

// KindaCode.com
// Define a reusable function
const formatCurrency = (amount, currency) => {
  return new Intl.NumberFormat('en-US', {
    style: 'currency',
    currency: currency,
  }).format(amount);
};

// Try it
console.log(formatCurrency(1000000, 'USD'));
console.log(formatCurrency(12000, 'EUR'));
console.log(formatCurrency(500000000000, 'JPY'));

Output:

$1,000,000.00
€12,000.00
¥500,000,000,000

Example of using Number.toLocaleString:

// US Dollar
const usd = 300000;
console.log(
  usd.toLocaleString('en-US', { style: 'currency', currency: 'USD' })
);

// India Rupee
const inr = 1700000000;
console.log(
  inr.toLocaleString('hi-IN', { style: 'currency', currency: 'INR' })
);


// Vietnam Dong
const vnd = 2300000;
console.log(
  vnd.toLocaleString('vi-VN', { style: 'currency', currency: 'VND' })
);

Output:

$300,000.00
₹1,70,00,00,000.00
2.300.000 ₫

I hope the examples above help you in some ways. Learn the basic things deeply, and you will write code much faster and avoid a mountain of blunders. Further reading:

You can also check out our Javascript category page, TypeScript category page, Node.js category page, and React category page for the latest tutorials and examples.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

You May Also Like