The reference cited in the original answer below was wrong. There is a built in function for this, which is exactly what kaiser suggests below: toLocaleString

So you can do:

(1234567.89).toLocaleString('en')              // for numeric input
parseFloat("1234567.89").toLocaleString('en')  // for string input

The function implemented below works, too, but simply isn't necessary.

(I thought perhaps I'd get lucky and find out that it was necessary back in 2010, but no. According to this more reliable reference, toLocaleString has been part of the standard since ECMAScript 3rd Edition [1999], which I believe means it would have been supported as far back as IE 5.5.)


Original Answer

According to this reference there isn't a built in function for adding commas to a number. But that page includes an example of how to code it yourself:

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

Edit: To go the other way (convert string with commas to number), you could do something like this:

parseFloat("1,234,567.89".replace(/,/g,''))
Answer from Tim Goodman on Stack Overflow
Top answer
1 of 15
246

The reference cited in the original answer below was wrong. There is a built in function for this, which is exactly what kaiser suggests below: toLocaleString

So you can do:

(1234567.89).toLocaleString('en')              // for numeric input
parseFloat("1234567.89").toLocaleString('en')  // for string input

The function implemented below works, too, but simply isn't necessary.

(I thought perhaps I'd get lucky and find out that it was necessary back in 2010, but no. According to this more reliable reference, toLocaleString has been part of the standard since ECMAScript 3rd Edition [1999], which I believe means it would have been supported as far back as IE 5.5.)


Original Answer

According to this reference there isn't a built in function for adding commas to a number. But that page includes an example of how to code it yourself:

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

Edit: To go the other way (convert string with commas to number), you could do something like this:

parseFloat("1,234,567.89".replace(/,/g,''))
2 of 15
139

If is about localizing thousands separators, delimiters and decimal separators, go with the following:

// --> numObj.toLocaleString( [locales [, options] ] )
parseInt( number ).toLocaleString();

There are several options you can use (and even locales with fallbacks):

number = 123456.7089;

result  = parseInt( number ).toLocaleString() + "<br>";
result += number.toLocaleString( 'de-DE' ) + "<br>";
result += number.toLocaleString( 'ar-EG' ) + "<br>";
result += number.toLocaleString( 'ja-JP', { 
  style           : 'currency',
  currency        : 'JPY',
  currencyDisplay : 'symbol',
  useGrouping     : true
} ) + "<br>";
result += number.toLocaleString( [ 'jav', 'en' ], { 
  localeMatcher            : 'lookup',
  style                    : 'decimal',
  minimumIntegerDigits     : 2,
  minimumFractionDigits    : 2,
  maximumFractionDigits    : 3,
  minimumSignificantDigits : 2,
  maximumSignificantDigits : 3
} ) + "<br>";

var el = document.getElementById( 'result' );
el.innerHTML = result;
<div id="result"></div>

Details on the MDN info page.

Edit: Commentor @I like Serena adds the following:

To support browsers with a non-English locale where we still want English formatting, use value.toLocaleString('en'). Also works for floating point.

🌐
GitHub
gist.github.com › fjaguero › 6932045
JS Regex: Adds thousands separator to a number. · GitHub
This works just fine. "1234567.89" turns into "1.234.567.89" due to the separator is ".", replacing it with "," turns the first value to "1,234,567.89" ... https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString
Discussions

javascript - How to get Decimal and Thousand separator of toLocaleString() method? - Stack Overflow
28 JavaScript: Format whole numbers using toLocaleString() More on stackoverflow.com
🌐 stackoverflow.com
javascript - Chrome - toLocaleString() - Thousands separator is not working for Spanish - Stack Overflow
In Chrome, when the locale is set in 'es' the thousands separator is not there. ... (2500).toLocaleString('en') "2,500" (2500).toLocaleString('pt') "2.500" (2500).toLocaleString('es') "2500" (25000).toLocaleString('es') "25.000" More on stackoverflow.com
🌐 stackoverflow.com
Add dot (.) as a thousands separator as the user types on input
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString More on reddit.com
🌐 r/learnjavascript
15
1
August 26, 2022
How to determine thousands separator in JavaScript - Stack Overflow
In order for toLocaleString to work, the browser/JavaScript must know the user's locale and whether or not the specific locale uses "," or "." for a thousands separator. Is it possible to access this More on stackoverflow.com
🌐 stackoverflow.com
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Number › toLocaleString
Number.prototype.toLocaleString() - JavaScript | MDN
const number = 123456.789; // German uses comma as decimal separator and period for thousands console.log(number.toLocaleString("de-DE")); // 123.456,789 // Arabic in most Arabic speaking countries uses Eastern Arabic digits console.log(number.toLocaleString("ar-EG")); // ١٢٣٤٥٦٫٧٨٩ // India uses thousands/lakh/crore separators console.log(number.toLocaleString("en-IN")); // 1,23,456.789 // the nu extension key requests a numbering system, e.g.
🌐
Adobe Support Community
community.adobe.com › home › after effects › bugs
Wrong thousand separator in num.toLocaleString("de-CH") - should be apostrophe not single quote
May 23, 2023 - This appears to be an issue within the V8 JavaScript engine, the Montserrat typeface itself, or perhaps the glyphs Montserrat assigns within Adobe products. E.g. if I run "(12345).toLocaleString("de-CH")" in the JS Console of Chrome, the return value is '12’345', using the curved single-quote for the thousands-separator rather than the apostrophe.
Find elsewhere
🌐
Reddit
reddit.com › r/learnjavascript › add dot (.) as a thousands separator as the user types on input
r/learnjavascript on Reddit: Add dot (.) as a thousands separator as the user types on input
August 26, 2022 -

I have a text type input and I’d like for it to show thousands separator with a dot (.) (NOT comma). For example, 1.200 or 125.500

I’ve tried this with a onkeyup event, and I added a regular expression I found online, but the problem is, it adds unnecessary dots. For example, I’ll recreate a user inputting a number:

1 12 120 1.200 1.2.000 1.2.0.000

It’s like it applies it continually but I want the final result to be: 120.000

Is there a way to do this? Maybe it’s not with an onkeyup event on the input tag, maybe it’s something else. But I can’t think of a way.

Thanks. If you need, I’ll look up the regex code I used.

Top answer
1 of 3
5

A little further digging and I found Intl.NumberFormat. I think this is more elegant...

const thousandsSeparator = (function(){
    if (typeof Intl !== 'object') {
        return ','; // fallback
    }
    // Get the formatting object for your locale
    const numFormat = new Intl.NumberFormat();
    // The resolved.pattern will be something like "#,##0.###"
    return numFormat.resolved.pattern.substr(1,1);
})();

Or if you really need it ultra-concise...

const thousandsSeparator = (Intl) ? (new Intl.NumberFormat()).resolved.pattern.substr(1,1) : ",";

Compatibility warning (2015):

  • The Intl object may not be supported in Safari for some reason -- http://caniuse.com/#feat=internationalization -- despite it being part of standard ECMAScript.
  • While the Intl object may exist in some ECMAScript-standard browsers, the code above will only work in Chrome.
  • Sadly Firefox 40 and IE 11 currently do not have a resolved property in numFormat.

An elegant cross-browser solution is still out there...

Update (2021):

Intl, and numFormat.resolved may have better browser support in non-Chrome browsers now. See comments for latest information.

2 of 3
1

Easy, I guess. This should help you to start with this ES6 solution

function getThousandSeparator(locale){
  const mapperRE = /(.{1})\d{3}(\D)\d$/,
    digitRE = /\d/,
    formattedValue = (new Intl.NumberFormat(locale?? navigator.language)).format(1000.1);
  let [_,thousand, decimal] = mapperRE.exec(formattedValue) ?? [null,null,null];
  //In case the captured position is number it means there's no thousand separator
  if(digitRE.test(thousand)) 
    thousand = '';
  return {thousand, decimal}
}
🌐
Codingexercises
codingexercises.com › format-a-number-with-a-comma-as-a-thousands-separator-js
Format a number with comma as a thousands separator in JS
January 12, 2021 - In JS, we can add a comma as thousands separator using the toLocaleString() method, or using Intl.NumberFormat().format, or using a RegExp. In this tutorial, we'll cover the first two listed means of doing this.
🌐
W3Schools
w3schools.com › jsref › jsref_tolocalestring_number.asp
JavaScript Number toLocaleString() Method
The toLocaleString() returns a number as a string, using local language format.
🌐
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 - Additional examples: console.log(num.toLocaleString("en-US", { minimumFractionDigits: 2 })); // "1,234,567.89" console.log(num.toLocaleString("de-DE")); // "1.234.567,89" (German locale) Using Intl.NumberFormat A more advanced method for custom formatting: const formatter = new Intl.NumberFormat("en-US"); console.log(formatter.format(1234567.89)); // Output: "1,234,567.89" Handling Large Numbers JavaScript has a maximum safe integer value (9007199254740991).
🌐
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 - </b> <p>Output for '123456789': <span class="output"></span> </p> <button onclick="separateNumber()">Separate thousands</button> <script type="text/javascript"> function separateNumber() { givenNumber = 123456789; nfObject = new Intl.NumberFormat('en-US'); output = nfObject.format(givenNumber); document.querySelector('.output').textContent = output; } </script> ... The toLocaleString() method is used to return a string with a language-sensitive representation of a number.
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 ====>"} ${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 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 ====>"} ${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.

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.

🌐
IncludeHelp
includehelp.com › code-snippets › how-to-print-a-number-with-commas-as-thousands-separators-in-javascript.aspx
How to print a number with commas as thousands separators in JavaScript?
October 20, 2022 - For separating the number with commas as thousand separators, we can use toLocalString() method.The toLocalString() is a built-in function in JavaScript. The optional locales parameter is used to specify the format of the number. The locale 'en-US' is used to specify that the locale takes the ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › BigInt › toLocaleString
BigInt.prototype.toLocaleString() - JavaScript - MDN - Mozilla
October 30, 2025 - const bigint = 123456789123456789n; // German uses period for thousands console.log(bigint.toLocaleString("de-DE")); // 123.456.789.123.456.789 // Arabic in most Arabic speaking countries uses Eastern Arabic digits console.log(bigint.toLocaleString("ar-EG")); // ١٢٣٬٤٥٦٬٧٨٩٬١٢٣٬٤٥٦٬٧٨٩ // India uses thousands/lakh/crore separators console.log(bigint.toLocaleString("en-IN")); // 1,23,45,67,89,12,34,56,789 // the nu extension key requests a numbering system, e.g.
🌐
Droidscript
droidscript.org › javascript › Global_Objects › Number › toLocaleString().html
numObj.toLocaleString()
March 23, 2024 - var number = 123456.789; // German uses comma as decimal separator and period for thousands console.log(number.toLocaleString('de-DE')); // → 123.456,789 // Arabic in most Arabic speaking countries uses Eastern Arabic digits console.log(number.toLocaleString('ar-EG')); // → ١٢٣٤٥٦٫٧٨٩ // India uses thousands/lakh/crore separators console.log(number.toLocaleString('en-IN')); // → 1,23,456.789 // the nu extension key requests a numbering system, e.g.
🌐
Community
community.plumsail.com › forms
Thousands Separator - Forms - Community
June 19, 2024 - Hi, I have a form with 7 currency fields. The form-filler will input one value, and the other 6 are calculated by javascript. It works & looks good on the form, but the result document doesn't take the formatting with the thousands separator. In the javascript, I'm using .toString() to convert ...