Format Numbers With Commas In JavaScript

31st July 2008 - 4 minutes read time

I found a good function that details how to format numbers with commas in JavaScript and thought I would reproduce it here. It basically takes any number and turns it into formatted string with the thousands separated by commas. The number_format() function is available in PHP and I have been relying on PHP to sort out the formatting before returning it via an AJAX call. I can now include this function into my JavaScript library and do this on the client side.

The function works by using regular expressions to first split the string into whole number and decimal, before splitting the number by groups of three digits.

The regular expression used is (\d+)(\d{3}), which looks for a sequence of three numbers preceded by any numbers. This is a little trick that causes the regular expression engine to work from right to left, instead of left to right.

  1. function addCommas(nStr){
  2. nStr += '';
  3. var x = nStr.split('.');
  4. var x1 = x[0];
  5. var x2 = x.length > 1 ? '.' + x[1] : '';
  6. var rgx = /(\d+)(\d{3})/;
  7. while (rgx.test(x1)) {
  8. x1 = x1.replace(rgx, '$1' + ',' + '$2');
  9. }
  10. return x1 + x2;
  11. }

This causes a comma to be added to every third numeric character. So for the number 123456 the string 123,456 is returned. Here are some more examples.

  1. addCommas(1000); // 1,000
  2. addCommas(12345); // 12,345
  3. addCommas(1234567890); // 1,234,567,890
  4. addCommas(12345.1234); // 12,345.1234

The site also details the creation of formatted numbers with the inclusion of different parameters. This means that instead of using a comma to separate block of three numbers you can use anything.

  1. function addSeparatorsNF(nStr, inD, outD, sep){
  2. nStr += '';
  3. var dpos = nStr.indexOf(inD);
  4. var nStrEnd = '';
  5. if (dpos != -1) {
  6. nStrEnd = outD + nStr.substring(dpos + 1, nStr.length);
  7. nStr = nStr.substring(0, dpos);
  8. }
  9. var rgx = /(\d+)(\d{3})/;
  10. while (rgx.test(nStr)) {
  11. nStr = nStr.replace(rgx, '$1' + sep + '$2');
  12. }
  13. return nStr + nStrEnd;
  14. }

This function was created because not every number is formatted in the same way. For example, the number 1234 might be formatted as 1,234 or even 1.234.

The function takes the following arguments

  • nStr : This is the number to be formatted. This might be a number or a string. No validation is done on this input.
  • inD : This is the decimal character for the string. This is usually a dot but might be something else.
  • outD : This is what to change the decimal character into.
  • sep : This is the separator, which is usually a comma.

The function takes the input string and splits it into two parts based on the decimal point character. The same regular expression is used on the string before the decimal point as in the previous function, the major difference is that the separator is taken from the function arguments.

Here are a few examples.

  1. alert(addSeparatorsNF(1234567890,'.','.',',')); // 1,234,567,890
  2. alert(addSeparatorsNF(12345.1234,'.','POINT','COMMA')); // 12COMMA345POINT1234
  3. alert(addSeparatorsNF(12345.1234,'5','POINT','COMMA')); // 1COMMA234POINT.1234
  4. alert(addSeparatorsNF('1234,56',',','.',',')); // 1,234.56


You are inadvertently using globals x, x1, and x2. Use "var" before each is initialized.

Collin Stocks (Tue, 11/25/2014 - 22:58)

Thank You, this helped a lot!

InCode (Sun, 10/30/2016 - 15:11)

Great tutorial. This tutorial was really helpful to known the number format with commas in PHP. Put this code on my business site and it works. Thanks.

Php Developer (Tue, 06/20/2017 - 05:38)

As a php developer, i don't know why i feel JavaScript is a hard to learn language

Jamie (Fri, 06/30/2017 - 11:56)


Thank you , though a old post , but it still helps for non-regular coders like me , i used to do quick prototyping before real development.

Thank u !

Akash (Sat, 04/25/2020 - 11:22)


Thanks! This helped my project Shoppie a lot.

Tim Sankara (Sun, 03/07/2021 - 02:43)


great - simple and effective.

Enock Oloo (Sat, 03/27/2021 - 11:29)

Add new comment

The content of this field is kept private and will not be shown publicly.