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 how I am doing it:
function numberWithCommas(x) {
x = x.toString();
var pattern = /(-?\d+)(\d{3})/;
while (pattern.test(x))
x = x.replace(pattern, "$1,$2");
return x;
}
console.log(numberWithCommas(1000))
Is there a simpler or more elegant way to do it? It would be nice if it works with floats also, but that is not necessary. It does not need to be locale-specific to decide between periods and commas.
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:
The regex uses 2 lookahead assertions:
For example, if you pass it
123456789.01, the positive assertion will match every spot to the left of the 7 (since789is a multiple of 3 digits,678is a multiple of 3 digits,567, etc.). The negative assertion checks that the multiple of 3 digits does not have any digits after it.789has a period after it so it is exactly a multiple of 3 digits, so a comma goes there.678is a multiple of 3 digits but it has a9after it, so those 3 digits are part of a group of 4, and a comma does not go there. Similarly for567.456789is 6 digits, which is a multiple of 3, so a comma goes before that.345678is a multiple of 3, but it has a9after it, so no comma goes there. And so on. The\Bkeeps 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:
@t.j.crowder pointed out that now that JavaScript has lookbehind (support info), it can be solved in the regular expression itself:
(?<!\.\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 thesplitandjoinsolution (comparison), at least in V8.