andrew4582

addCommas

Dec 2nd, 2011
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function addCommas(nStr) {
  2.         nStr += '';
  3.         x = nStr.split('.');
  4.         x1 = x[0];
  5.         x2 = x.length > 1 ? '.' + x[1] : '';
  6.         var rgx = /(\d+)(\d{3})/;
  7.         while (rgx.test(x1)) {
  8.             x1 = x1.replace(rgx, '$1' + ',' + '$2');
  9.         }
  10.         return x1 + x2;
  11.     }
Advertisement
Add Comment
Please, Sign In to add comment