Posted By

fengelz on 09/14/11


Tagged

number format


Versions (?)

format thousands


 / Published in: JavaScript
 

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

  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  

You need to login to post a comment.