formatting - How to print a number with commas as thousands separators in JavaScript

ID : 343

viewed : 77

Tags : javascriptformattingnumbersintegerjavascript

Top 5 Answer for formatting - How to print a number with commas as thousands separators in JavaScript

vote vote

100

I used the idea from Kerry's answer, but simplified it since I was just looking for something simple for my specific purpose. Here is what I have:

function numberWithCommas(x) {     return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); } 

function numberWithCommas(x) {     return x.toString().replace(/\B(?<!\.\d*)(?=(\d{3})+(?!\d))/g, ","); }  function test(x, expect) {     const result = numberWithCommas(x);     const pass = result === expect;     console.log(`${pass ? "✓" : "ERROR ====>"} ${x} => ${result}`);     return pass; }  let failures = 0; failures += !test(0,        "0"); failures += !test(100,      "100"); failures += !test(1000,     "1,000"); failures += !test(10000,    "10,000"); failures += !test(100000,   "100,000"); failures += !test(1000000,  "1,000,000"); failures += !test(10000000, "10,000,000"); if (failures) {     console.log(`${failures} test(s) failed`); } else {     console.log("All tests passed"); }
.as-console-wrapper {     max-height: 100% !important; }


The regex uses 2 lookahead assertions:

  • a positive one to look for any point in the string that has a multiple of 3 digits in a row after it,
  • a negative assertion to make sure that point only has exactly a multiple of 3 digits. The replacement expression puts a comma there.

For example, if you pass it 123456789.01, the positive assertion will match every spot to the left of the 7 (since 789 is a multiple of 3 digits, 678 is a multiple of 3 digits, 567, etc.). The negative assertion checks that the multiple of 3 digits does not have any digits after it. 789 has a period after it so it is exactly a multiple of 3 digits, so a comma goes there. 678 is a multiple of 3 digits but it has a 9 after it, so those 3 digits are part of a group of 4, and a comma does not go there. Similarly for 567. 456789 is 6 digits, which is a multiple of 3, so a comma goes before that. 345678 is a multiple of 3, but it has a 9 after it, so no comma goes there. And so on. The \B keeps the regex from putting a comma at the beginning of the string.

@neu-rah mentioned that this function adds commas in undesirable places if there are more than 3 digits after the decimal point. If this is a problem, you can use this function:

function numberWithCommas(x) {     var parts = x.toString().split(".");     parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");     return parts.join("."); } 

function numberWithCommas(x) {     var parts = x.toString().split(".");     parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");     return parts.join("."); }  function test(x, expect) {     const result = numberWithCommas(x);     const pass = result === expect;     console.log(`${pass ? "✓" : "ERROR ====>"} ${x} => ${result}`);     return pass; }  let failures = 0; failures += !test(0              , "0"); failures += !test(0.123456       , "0.123456"); failures += !test(100            , "100"); failures += !test(100.123456     , "100.123456"); failures += !test(1000           , "1,000"); failures += !test(1000.123456    , "1,000.123456"); failures += !test(10000          , "10,000"); failures += !test(10000.123456   , "10,000.123456"); failures += !test(100000         , "100,000"); failures += !test(100000.123456  , "100,000.123456"); failures += !test(1000000        , "1,000,000"); failures += !test(1000000.123456 , "1,000,000.123456"); failures += !test(10000000       , "10,000,000"); failures += !test(10000000.123456, "10,000,000.123456"); if (failures) {     console.log(`${failures} test(s) failed`); } else {     console.log("All tests passed"); }
.as-console-wrapper {     max-height: 100% !important; }

@t.j.crowder pointed out that now that JavaScript has lookbehind (support info), it can be solved in the regular expression itself:

function numberWithCommas(x) {     return x.toString().replace(/\B(?<!\.\d*)(?=(\d{3})+(?!\d))/g, ","); } 

function numberWithCommas(x) {     return x.toString().replace(/\B(?<!\.\d*)(?=(\d{3})+(?!\d))/g, ","); }  function test(x, expect) {     const result = numberWithCommas(x);     const pass = result === expect;     console.log(`${pass ? "✓" : "ERROR ====>"} ${x} => ${result}`);     return pass; }  let failures = 0; failures += !test(0,               "0"); failures += !test(0.123456,        "0.123456"); failures += !test(100,             "100"); failures += !test(100.123456,      "100.123456"); failures += !test(1000,            "1,000"); failures += !test(1000.123456,     "1,000.123456"); failures += !test(10000,           "10,000"); failures += !test(10000.123456,    "10,000.123456"); failures += !test(100000,          "100,000"); failures += !test(100000.123456,   "100,000.123456"); failures += !test(1000000,         "1,000,000"); failures += !test(1000000.123456,  "1,000,000.123456"); failures += !test(10000000,        "10,000,000"); failures += !test(10000000.123456, "10,000,000.123456"); if (failures) {     console.log(`${failures} test(s) failed`); } else {     console.log("All tests passed"); }
.as-console-wrapper {     max-height: 100% !important; }

(?<!\.\d*) is a negative lookbehind that says the match can't be preceded by a . followed by zero or more digits. The negative lookbehind is faster than the split and join solution (comparison), at least in V8.

vote vote

83

I'm surprised nobody mentioned Number.prototype.toLocaleString. It's implemented in JavaScript 1.5 (which was introduced in 1999) so it's basically supported across all major browsers.

var n = 34523453.345; console.log(n.toLocaleString());    // "34,523,453.345"

It also works in Node.js as of v0.12 via inclusion of Intl

If you want something different, Numeral.js might be interesting.

vote vote

80

Below are two different browser APIs that can transform Numbers into structured Strings. Keep in mind that not all users' machines have a locale that uses commas in numbers. To enforce commas to the output, any "western" locale may be used, such as en-US

let number = 1234567890; // Example number to be converted 

⚠️ Mind that javascript has a maximum integer value of 9007199254740991


toLocaleString

// default behaviour on a machine with a local that uses commas for numbers let number = 1234567890; number.toLocaleString(); // "1,234,567,890"  // With custom settings, forcing a "US" locale to guarantee commas in output let number2 = 1234.56789; // floating point example number2.toLocaleString('en-US', {maximumFractionDigits:2}) // "1,234.57" 

NumberFormat

let number = 1234567890; let nf = new Intl.NumberFormat('en-US'); nf.format(number); // "1,234,567,890" 

From what I checked (Firefox at least) they are both more or less same regarding performance.

Live demo: https://codepen.io/vsync/pen/MWjdbgL?editors=1000

vote vote

65

I suggest using phpjs.org 's number_format()

function number_format(number, decimals, dec_point, thousands_sep) {     // http://kevin.vanzonneveld.net     // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)     // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)     // +     bugfix by: Michael White (http://getsprink.com)     // +     bugfix by: Benjamin Lupton     // +     bugfix by: Allan Jensen (http://www.winternet.no)     // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)     // +     bugfix by: Howard Yeend     // +    revised by: Luke Smith (http://lucassmith.name)     // +     bugfix by: Diogo Resende     // +     bugfix by: Rival     // +      input by: Kheang Hok Chin (http://www.distantia.ca/)     // +   improved by: davook     // +   improved by: Brett Zamir (http://brett-zamir.me)     // +      input by: Jay Klehr     // +   improved by: Brett Zamir (http://brett-zamir.me)     // +      input by: Amir Habibi (http://www.residence-mixte.com/)     // +     bugfix by: Brett Zamir (http://brett-zamir.me)     // +   improved by: Theriault     // +   improved by: Drew Noakes     // *     example 1: number_format(1234.56);     // *     returns 1: '1,235'     // *     example 2: number_format(1234.56, 2, ',', ' ');     // *     returns 2: '1 234,56'     // *     example 3: number_format(1234.5678, 2, '.', '');     // *     returns 3: '1234.57'     // *     example 4: number_format(67, 2, ',', '.');     // *     returns 4: '67,00'     // *     example 5: number_format(1000);     // *     returns 5: '1,000'     // *     example 6: number_format(67.311, 2);     // *     returns 6: '67.31'     // *     example 7: number_format(1000.55, 1);     // *     returns 7: '1,000.6'     // *     example 8: number_format(67000, 5, ',', '.');     // *     returns 8: '67.000,00000'     // *     example 9: number_format(0.9, 0);     // *     returns 9: '1'     // *    example 10: number_format('1.20', 2);     // *    returns 10: '1.20'     // *    example 11: number_format('1.20', 4);     // *    returns 11: '1.2000'     // *    example 12: number_format('1.2000', 3);     // *    returns 12: '1.200'     var n = !isFinite(+number) ? 0 : +number,          prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),         sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,         dec = (typeof dec_point === 'undefined') ? '.' : dec_point,         toFixedFix = function (n, prec) {             // Fix for IE parseFloat(0.55).toFixed(0) = 0;             var k = Math.pow(10, prec);             return Math.round(n * k) / k;         },         s = (prec ? toFixedFix(n, prec) : Math.round(n)).toString().split('.');     if (s[0].length > 3) {         s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);     }     if ((s[1] || '').length < prec) {         s[1] = s[1] || '';         s[1] += new Array(prec - s[1].length + 1).join('0');     }     return s.join(dec); } 

UPDATE 02/13/14

People have been reporting this doesn't work as expected, so I did a JS Fiddle that includes automated tests.

Update 26/11/2017

Here's that fiddle as a Stack Snippet with slightly modified output:

function number_format(number, decimals, dec_point, thousands_sep) {      // http://kevin.vanzonneveld.net      // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)      // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)      // +     bugfix by: Michael White (http://getsprink.com)      // +     bugfix by: Benjamin Lupton      // +     bugfix by: Allan Jensen (http://www.winternet.no)      // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)      // +     bugfix by: Howard Yeend      // +    revised by: Luke Smith (http://lucassmith.name)      // +     bugfix by: Diogo Resende      // +     bugfix by: Rival      // +      input by: Kheang Hok Chin (http://www.distantia.ca/)      // +   improved by: davook      // +   improved by: Brett Zamir (http://brett-zamir.me)      // +      input by: Jay Klehr      // +   improved by: Brett Zamir (http://brett-zamir.me)      // +      input by: Amir Habibi (http://www.residence-mixte.com/)      // +     bugfix by: Brett Zamir (http://brett-zamir.me)      // +   improved by: Theriault      // +   improved by: Drew Noakes      // *     example 1: number_format(1234.56);      // *     returns 1: '1,235'      // *     example 2: number_format(1234.56, 2, ',', ' ');      // *     returns 2: '1 234,56'      // *     example 3: number_format(1234.5678, 2, '.', '');      // *     returns 3: '1234.57'      // *     example 4: number_format(67, 2, ',', '.');      // *     returns 4: '67,00'      // *     example 5: number_format(1000);      // *     returns 5: '1,000'      // *     example 6: number_format(67.311, 2);      // *     returns 6: '67.31'      // *     example 7: number_format(1000.55, 1);      // *     returns 7: '1,000.6'      // *     example 8: number_format(67000, 5, ',', '.');      // *     returns 8: '67.000,00000'      // *     example 9: number_format(0.9, 0);      // *     returns 9: '1'      // *    example 10: number_format('1.20', 2);      // *    returns 10: '1.20'      // *    example 11: number_format('1.20', 4);      // *    returns 11: '1.2000'      // *    example 12: number_format('1.2000', 3);      // *    returns 12: '1.200'      var n = !isFinite(+number) ? 0 : +number,           prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),          sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,          dec = (typeof dec_point === 'undefined') ? '.' : dec_point,          toFixedFix = function (n, prec) {              // Fix for IE parseFloat(0.55).toFixed(0) = 0;              var k = Math.pow(10, prec);              return Math.round(n * k) / k;          },          s = (prec ? toFixedFix(n, prec) : Math.round(n)).toString().split('.');      if (s[0].length > 3) {          s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);      }      if ((s[1] || '').length < prec) {          s[1] = s[1] || '';          s[1] += new Array(prec - s[1].length + 1).join('0');      }      return s.join(dec);  }    var exampleNumber = 1;  function test(expected, number, decimals, dec_point, thousands_sep)  {      var actual = number_format(number, decimals, dec_point, thousands_sep);      console.log(          'Test case ' + exampleNumber + ': ' +          '(decimals: ' + (typeof decimals === 'undefined' ? '(default)' : decimals) +          ', dec_point: "' + (typeof dec_point === 'undefined' ? '(default)' : dec_point) + '"' +          ', thousands_sep: "' + (typeof thousands_sep === 'undefined' ? '(default)' : thousands_sep) + '")'      );      console.log('  => ' + (actual === expected ? 'Passed' : 'FAILED') + ', got "' + actual + '", expected "' + expected + '".');      exampleNumber++;  }    test('1,235',    1234.56);  test('1 234,56', 1234.56, 2, ',', ' ');  test('1234.57',  1234.5678, 2, '.', '');  test('67,00',    67, 2, ',', '.');  test('1,000',    1000);  test('67.31',    67.311, 2);  test('1,000.6',  1000.55, 1);  test('67.000,00000', 67000, 5, ',', '.');  test('1',        0.9, 0);  test('1.20',     '1.20', 2);  test('1.2000',   '1.20', 4);  test('1.200',    '1.2000', 3);
.as-console-wrapper {    max-height: 100% !important;  }

vote vote

57

This is a variation of @mikez302's answer, but modified to support numbers with decimals (per @neu-rah's feedback that numberWithCommas(12345.6789) -> "12,345.6,789" instead of "12,345.6789"

function numberWithCommas(n) {     var parts=n.toString().split(".");     return parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",") + (parts[1] ? "." + parts[1] : ""); } 

Top 3 video Explaining formatting - How to print a number with commas as thousands separators in JavaScript

Related QUESTION?