format thousands


/ Published in: JavaScript
Save to your folder(s)

Thanks to http://www.mredkj.com/javascript/numberFormat.html#addcommas


Copy this code and paste it in your HTML
  1. formatNumber = function (number) {
  2. number = String(number).replace(new RegExp(/\s/g), '');
  3. var length = number.length;
  4. var x = number.split('.');
  5. var x1 = x[0];
  6. var x2 = x.length > 1 ? '.' + x[1] : '';
  7. var rgx = /(\d+)(\d{3})/;
  8. while (rgx.test(x1)) {
  9. x1 = x1.replace(rgx, '$1' + '.' + '$2');
  10. }
  11. return x1 + x2;
  12. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.