Try:
Option 1:
function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
Option 2:
Use
Number.prototype.toLocaleString[
^].
It was implemented in JavaScript 1.5 (introduced way early in 1999) so should be supported across by all browsers.
var n = 12345678.123
n.toLocaleString()