3 Ways To Add Comma To Numbers In Javascript – Thousands Separator

Welcome to a short tutorial on how to add commas to numbers in Javascript. So you have a project that needs to display some “nice-looking numbers”?

The easy ways to add commas to numbers in Javascript are:

  1. Use the toLocaleString() function
    • var num = 1234567.89;
    • var commas = num.toLocaleString("en-US");
  2. Convert the number to a string, and use a regular expression replace.
    • var commas = num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");

That covers the basics, but let us walk through more examples in this guide – Read on!

ⓘ I have included a zip file with all the example source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes Adding Comma Useful Bits & Links
Tutorial Video The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

EXAMPLE CODE DOWNLOAD

Click here to download the source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

QUICK NOTES

If you spot a bug, please feel free to comment below. I try to answer questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

 

ADD COMMAS TO NUMBERS

All right, let us now get into the examples of adding commas to a number in Javascript.

 

1) LOCALE STRING

1-locale.html
// (A) THE NUMBER
var num = 1234567.89;
 
// (B) TO LOCALE STRING
console.log(num.toLocaleString("en-US")); // 1,234,567.89
console.log(num.toLocaleString("fr-FR")); // 1 234 567,89
console.log(num.toLocaleString("ja-JP", { style: "currency", currency: "JPY" }));

As in the introduction, toLocaleString("en-US") is one of the easiest ways to add commas to a number. For beginners, that en-US fr-FR ja-JP is the language and locale code.

P.S. I have another tutorial to format the number as a currency, link below.

 

 

2) REGULAR EXPRESSION REPLACE

2-replace.html
// CREDIT: https://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript
// (A) THE NUMBER 
var num = 1234567.89;
 
// (B) CONVERT TO STRING & REPLACE
var commas = num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
console.log(commas); // 1,234,567.89

Once upon a time in the Dark Ages of the Internet, this is how we roll. Basically, convert the number into a string, then use a pattern (regular expression) to insert the commas. Not going into the details of how regular expression work, as it is a can of worms. But replace(/\B(?=(\d{3})+(?!\d))/g, ",") does the magic of inserting the commas.

 

 

3) MANUAL INSERT COMMA

3-manual.html
// (A) ADD COMMAS TO SEPERATE GIVEN NUMBER
// NUM : ORIGINAL NUMBER
// PER : ADD COMMA PER X DIGITS
function addComma (num, per) {
  // (A1) CONVERT NUMBER TO STRING
  var cString = num.toString();

  // (A2) SPLIT WHOLE & DECIMAL NUMBERS
  var cWhole = "", cDec = "",
  cDot = cString.indexOf(".");
  if (cDot == -1) { cWhole = cString; }
  else {
    cWhole = cString.substring(0, cDot);
    cDec = cString.substring(cDot);
  }
 
  // (A3) ADD COMMAS TO WHOLE NUMBER
  var aComma = "", count = 0;
  if (cWhole.length > per) {
    for (let i=(cWhole.length-1); i>=0; i--) {
      aComma = cWhole.charAt(i) + aComma;
      count++;
      if (count == per && i!=0) {
        aComma = "," + aComma; 
        count = 0;
      }
    }
  }
 
  // (A4) RETURN "WHOLE WITH COMMA" PLUS DECIMAL PLACES
  return aComma + cDec;
}
 
// (B) TEST
// (B1) WHOLE NUMBER
var first = addComma(123456789, 3);
console.log(first); // 123,456,789

// (B2) DECIMAL
var second = addComma(23456.78, 3);
console.log(second); // 23,456.78

Lastly, this is another manual alternative to adding commas to a number –

  • A1 – Convert the number into a string.
  • A2 – Split the number into 2 sections. The whole numbers and decimals.
  • A3 – Only add commas to the portion of the whole number.

 

 

USEFUL BITS & LINKS

That’s all for this tutorial, and here is a small section on some extras and links that may be useful to you.

 

SUMMARY

Function Description Link
NUMBER.toLocaleString() Format the number to the specified locale. Click Here
NUMBER.toString() Converts a number to a string. Click Here
STRING.replace() Replace all instances of a matched pattern in the string. Click Here

 

LINKS & REFERENCES

 

TUTORIAL VIDEO

 

INFOGRAPHIC CHEAT SHEET

Add Comma To Numbers In Javascript (Click To Enlarge)

 

THE END

Thank you for reading, and we have come to the end of this guide. I hope that it has helped you with your project, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

2 thoughts on “3 Ways To Add Comma To Numbers In Javascript – Thousands Separator”

  1. Adds commas after the decimal for longer numbers.

    // (A) THE NUMBER
    var num = 1234.56789;
    // (B) CONVERT TO STRING & REPLACE
    var commas = num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, “,”);
    console.log(commas); // 1,234.56,789

Leave a Comment

Your email address will not be published. Required fields are marked *