Did you add the locale to the toLocaleString function? for example javascript var number = 12345.543; number.toLocaleString('en') if i print the above code in the console it shows "12,345.543" Answer from Richard Price on teamtreehouse.com
🌐
Favtutor
favtutor.com β€Ί articles β€Ί format-numbers-commas-javascript
Format Numbers with Commas in JavaScript (with code)
February 5, 2024 - Learn how to format numbers by adding commas using JavaScript using the toLocaleString() method and using regular expressions.
🌐
Medium
medium.com β€Ί @onlinemsr β€Ί big-numbers-no-worries-javascript-format-number-with-commas-17ec7f878834
Big Numbers, No Worries: JavaScript Format Number With Commas
March 23, 2024 - Learn how to use JavaScript format numbers with commas to display numbers in a readable way. An easy and practical guide.
🌐
DEV Community
dev.to β€Ί onlinemsr β€Ί big-numbers-no-worries-javascript-format-number-with-commas-n6j
Big Numbers, No Worries: JavaScript Format Number With Commas - DEV Community
March 23, 2024 - If you are a web developer, you ... number, like 1000000 or 1,000,000, for better readability. Using the toLocaleString() method you can format numbers with commas....
Top answer
1 of 16
3954

I used the idea from Kerry's answer, but I 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 ====>"} {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 two 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 ====>"} {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 ====>"} {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.

2 of 16
3275

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.

🌐
Byby
byby.dev β€Ί js-format-numbers-commas
How to format numbers with commas in JavaScript
There are different ways to format numbers with commas and decimal places in JavaScript, depending on your needs and preferences.
🌐
Medium
medium.com β€Ί @noffybarudwale β€Ί javascript-format-numbers-with-commas-and-decimals-86b68ec5b180
JavaScript : Format numbers with commas and decimals. | by Nofij Barudwale | Medium
October 13, 2021 - 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.
🌐
Stack Abuse
stackabuse.com β€Ί bytes β€Ί format-numbers-with-commas-in-javascript
Format Numbers with Commas in JavaScript
August 7, 2023 - Formatting numbers with commas is a common requirement, enhancing the readability and overall user experience. While JavaScript provides native methods like toLocaleString, you can also achieve this through regular expressions or even employ libraries like Lodash.
Find elsewhere
🌐
W3Resource
w3resource.com β€Ί javascript-exercises β€Ί javascript-math-exercise-39.php
JavaScript Math: Print an integer with commas as thousands separators - w3resource
Write a JavaScript function to print an integer with thousands separated by commas. Test Data: console.log(thousands_separators(1000)); "1,000" console.log(thousands_separators(10000.23)); "10,000.23" console.log(thousands_separators(100000)); ...
🌐
CSS-Tricks
css-tricks.com β€Ί snippets β€Ί javascript β€Ί comma-values-in-numbers
Put Comma Values in Numbers | CSS-Tricks
December 19, 2009 - Javascript has a method called toFixed() that will format your numbers so they have commas and even let you add 2 decimals. So if you have a number like 12345678.90, .toFixed() will convert it to 12,345,678.90.
🌐
sebhastian
sebhastian.com β€Ί javascript-format-number-commas
JavaScript format number with commas (example included) | sebhastian
July 8, 2022 - Formatting a number using regex ... a huge request to format numbers with commas, it’s probably better to use regex pattern and String.replace() method instead of toLocaleString() method....
🌐
MSR
rajamsr.com β€Ί home β€Ί javascript format number with commas: 5 best ways
JavaScript Format Number with Commas: 5 Best Ways | MSR - Web Dev Simplified
March 19, 2024 - Learn how to use JavaScript format number with commas to display numbers in a readable way. An easy and practical guide.
🌐
Sabe
sabe.io β€Ί blog β€Ί javascript-format-numbers-commas
Format Numbers with Commas in JavaScript | Sabe
March 6, 2022 - The best way to format numbers in JavaScript is to use the toLocaleString() method. This method exists on the Number object and will return a string with the number formatted with commas.
🌐
Crio
crio.do β€Ί blog β€Ί format-numbers-with-commas-as-thousands-separators-2025-javascript-criodo
How to Format a Number with Commas as Thousands Separators?
December 26, 2024 - Formatting numbers with commas as thousands separators is essential for enhancing readability, especially when displaying large numbers in user interfaces. This is a common requirement for financial data, statistics, and other applications. Here’s a concise and effective way to format numbers using JavaScript:
🌐
EyeHunts
tutorial.eyehunts.com β€Ί home β€Ί javascript format number with commas and decimal places | example
JavaScript format number with commas and decimal places | Code
December 13, 2022 - A simple example code converts a given number into number value format with a comma and two decimal points. This turns a number 1234.567 in to 1,234.567. <!DOCTYPE html> <html> <body> <script> var n = 1234.567; var val = Math.round(Number(n) ...
🌐
DEV Community
dev.to β€Ί lavary β€Ί add-commas-to-numbers-in-javascript-explained-with-examples-27k8
Add commas to numbers in JavaScript (Explained with examples) - DEV Community
February 6, 2023 - This guide explores the three common ways to add commas to numbers in JavaScript. Displaying numbers – whether currency or plain numbers – in an easy-to-read format significantly improves your HTML page content and overall user experience.
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί javascript β€Ί how-to-print-a-number-with-commas-as-thousands-separators-in-javascript
How to print a number with commas as thousands separators in JavaScript? - GeeksforGeeks
July 12, 2025 - The format() 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.
🌐
HashBangCode
hashbangcode.com β€Ί article β€Ί format-numbers-commas-javascript
Format Numbers With Commas In JavaScript | #! code
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.
🌐
W3Schools
w3schools.com β€Ί js β€Ί js_number_methods.asp
JavaScript Number Methods
The valueOf() method is used internally in JavaScript to convert Number objects to primitive values.
🌐
TutorialsPoint
tutorialspoint.com β€Ί how-to-print-a-number-with-commas-as-thousands-of-separators-in-javascript
How to print a number with commas as thousands of separators in JavaScript?
In this program, the toLocaleString() returns the comma-separated number of the input. <html> <body> <p id="inp"></p> <p id="out"></p> <script> const num = 1234567890; document.getElementById("inp").innerHTML = "Input : " + num; const result = num.toLocaleString('en-US'); document.getElementById("out").innerHTML = "Output: " + result; </script> </body> </html> Intl is the internationalization namespace in JavaScript. Language-sensitive number formatting is one of the uses of this method.