How to Convert Numbers to Currency String in javascript

In Javascript applications, You can store data as numbers and display the data in currency strings.

For example, In a commerce web application, Each product is displayed with a price. Suppose, Application shows the dollar as currency to US users and displays it in Rupees to Indian users.

Every language provides Internalization API to display the localized formatted data.

Javascript provides default localization and Internalization API to convert localized string format. You can check my other post on Javascript check substring exists in a String

Intl.NumberFormat to convert Numbers into Currency String in Javascript

Intl.NumberFormat class used to convert to language-specific strings First Create a NumberFormat class

var formatter = new Intl.NumberFormat("en-US", {
  style: "currency",
  currency: "USD",
  //minimumFractionDigits: 0,
  //maximumFractionDigits: 0,
});

You need to provide these attributes

  • locale string which contains language code and country code, examples ie en-US, ie en-IN,de-DE,ja-JP
  • currency string if the style is currency, values are USD, EUR, and JPY, This displays the symbol of currency if style=unit values are liter, kilometer-per-hour
  • style : - currency,unit

You can check more about this class here🔗

Here is a complete example

const number = 100000;
console.log(
  new Intl.NumberFormat("en-US", {
    style: "currency",
    currency: "USD",
  }).format(number),
); //"$100,000.00"

console.log(
  new Intl.NumberFormat("de-DE", {
    style: "currency",
    currency: "EUR",
  }).format(number),
); //"100.000,00 €"

console.log(
  new Intl.NumberFormat("ja-JP", {
    style: "currency",
    currency: "JPY",
  }).format(number),
); // "¥100,000"

console.log(new Intl.NumberFormat("en-IN", {}).format(number));
//"1,00,000"

console.log(new Intl.NumberFormat("ar-EG").format(number));
// "100,000"

number format as currency string using toLocaleString in javascript

toLocaleString convert the number to a localized currency string.

Here is an example

const number = 1000000;

console.log(
  number.toLocaleString("en-US", {
    style: "currency",
    currency: "USD",
  }),
); //$1,000,000.00

console.log(
  number.toLocaleString("de-DE", {
    style: "currency",
    currency: "EUR",
  }),
); //1.000.000,00 €

console.log(
  number.toLocaleString("ja-JP", {
    style: "currency",
    currency: "JPY",
  }),
); // ¥1,000,000

console.log(
  number.toLocaleString("en-IN", {
    style: "currency",
    currency: "INR",
  }),
); //₹10,00,000.00