JavaScript : Format numbers with commas and decimals.

Nofij Barudwale
2 min readOct 1, 2021

--

Sometimes, you may need to format a number value with commas and decimals in your HTML pages to make it easier to read. Here I found both in one. You can transform a number value into a comma-separated with decimals by using JavaScript. Here are several ways to do that.

There are many different ways of printing an integer with a comma as a thousands separators in JavaScript. I found a good function that details how to and thought I would reproduce it here. It basically takes any number and turns it into formatted string with the thousands separated by commas and decimals.

Method 1: Using NumberFormatter()

  • The NumberFormat() object is used to represent numbers in a language-sensitive formatting. It can be used to represent currency or percentages according to the locale specified.
  • The locales parameter of this object is used to specify the format of the number. The ‘en-US’ locale is used to specify that the locale takes the format of the United States and the English language, where numbers are represented with a comma between the thousands.
  • The method of this object can be used to return a string of the number in the specified locale and formatting options. This will format the number with commas at the thousands of places and return a string with the formatted number.

The following example shows how it works:

  • value: the number which we want to add commas.
  • decimal: the number of digits after decimal points. It should be a number like 1,2 and 3 etc.
  • here you can specify Language. like ‘en-IN’ , ‘ja-JP’,’en-US’ and ‘de-DE’, etc.

Output will looks like:

--

--