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.
Discussions

javascript - How can I format a number with commas as thousands separators? - Stack Overflow
I am trying to print an integer in JavaScript with commas as thousands separators. For example, I want to show the number 1234567 as "1,234,567". How would I go about doing this? Here is ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How do I insert comma between numbers?
u/zappsg 's recommendation is a lot better, but for the purpose of learning, here's one way you could do it with some comments explaining how it works: function formatNumber(num) { // If the number is less than zero, make a note of this var isNegative = num < 0; // Convert the number to a string with two decimal palces, // then split that string into an array of characters. If the // number was lower than zero, throw away the minus sign var tempNumArray = isNegative ? num.toFixed(2).split('').slice(1) : num.toFixed(2).split(''); // We're gonna keep track of the period so we know not to add commas after it var dotIndex = tempNumArray.indexOf('.'); // The number of digits to the left of the decimal point // is 3 less than the length of the array var integerCount = tempNumArray.length - 3; // Map over every digit in the array var formattedArray = tempNumArray.map(function(digit, index, arr) { // Check if we're in the whole digits var isBeforeDot = (index + 1) < dotIndex; // If we're in the whole digits, check if the number of whole digits still left to go % 3 is 0. // If it is, we need to add a comma, so we'll return the current digit plus a comma. if (isBeforeDot && (integerCount - (index + 1)) % 3 === 0) { return digit + ','; } else { // Otherwise we just return the digit. return digit; } // Join our newly mapped array back into a string }).join(''); // If our number was negative return it with a minus sign in front, // otherwise just return the number return isNegative ? '-' + formattedArray : formattedArray; } Then, to use it, you'd just do: "$ " + formatNumber(var1 + var2); More on reddit.com
๐ŸŒ r/javascript
3
1
December 12, 2016
Format a Number with Commas in React
This is just vanilla JS, no? More on reddit.com
๐ŸŒ r/react
4
0
March 22, 2023
(I Found the solution) How to format numbers with commas/dots every 3 digits in formulas?
This is so awesome! Thank you very much More on reddit.com
๐ŸŒ r/Notion
10
23
January 29, 2024
๐ŸŒ
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.

๐ŸŒ
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.
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
Byby
byby.dev โ€บ js-format-numbers-commas
How to format numbers with commas in JavaScript
December 22, 2025 - There are different ways to format numbers with commas and decimal places in JavaScript, depending on your needs and preferences.
๐ŸŒ
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....
๐ŸŒ
W3Resource
w3resource.com โ€บ javascript-exercises โ€บ javascript-math-exercise-39.php
JavaScript Math: Print an integer with commas as thousands separators - w3resource
July 11, 2025 - 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)); ...
๐ŸŒ
Sabe
sabe.io โ€บ blog โ€บ javascript-format-numbers-commas
Format Numbers with Commas in JavaScript - Sabe.io
November 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.
๐ŸŒ
MSR
rajamsr.com โ€บ home โ€บ javascript format number with commas: 5 best ways
JavaScript Format Number with Commas: 5 Best Ways | MSR - Web Dev Simplified
September 26, 2019 - Learn how to use JavaScript format number with commas to display numbers in a readable way. An easy and practical guide.
๐ŸŒ
Crio
crio.do โ€บ blog โ€บ format-numbers-with-commas-as-thousands-separators-2025-javascript-criodo
How to Format a Number with Commas as Thousands Separators?
July 12, 2025 - 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:
๐ŸŒ
Code Boxx
code-boxx.com โ€บ home โ€บ 3 ways to add comma to numbers in javascript (thousands separator)
3 Ways To Add Comma To Numbers In Javascript (Thousands Separator)
July 9, 2024 - This tutorial will walk through how to add comma and thousands separator to numbers in Javascript. Free example code download included.
๐ŸŒ
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.
๐ŸŒ
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
March 23, 2024 - 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.
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
xjavascript
xjavascript.com โ€บ blog โ€บ how-to-format-a-number-with-commas-as-thousands-separators
How to Format Numbers with Commas as Thousands Separators in JavaScript: Simple & Elegant Methods โ€” xjavascript.com
April 1, 2015 - Itโ€™s the most robust and recommended method for formatting numbers with commas (or other separators, depending on the locale). Intl.NumberFormat is simple to use. By default, it uses the userโ€™s locale (or the environmentโ€™s default locale) to format numbers with thousands separators. ... const number = 1234567; const formatter = new Intl.NumberFormat(); // Uses default locale (e.g., 'en-US') const formattedNumber = formatter.format(number); console.log(formattedNumber); // Output: "1,234,567" (for 'en-US' locale)