I am wanting to display large numbers more nicely with commas. So if the number was say 123456789, it would display 123,456,789. I have looked around but I only found code t
There's a simpler syntax for toLocaleString:
Number(x).toLocaleString();
This lets you drop the number in as a variable instead of converting the number into an object.
This was answered here:
How to print a number with commas as thousands separators in JavaScript
In case you're not interested in reading the answer above, the code given was this:
function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
If you're using jquery use something like:
var val = parseInt($('#value').text());
//Use the code in the answer above to replace the commas.
val = numberWithCommas(val);
$('#value').text(val);
You can use toLocaleString:
num.toLocaleString('en', {useGrouping:true})