/ Published in: JavaScript
Thanks to http://www.mredkj.com/javascript/numberFormat.html#addcommas
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
formatNumber = function (number) { number = String(number).replace(new RegExp(/\s/g), ''); var length = number.length; var x = number.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, '$1' + '.' + '$2'); } return x1 + x2; }