trollolodn

#jquery: format num all controls html.. same class or id

Apr 18th, 2013
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //format number for all controls html of the same class or id
  2. $(function(){
  3.     arr = $('.col_3.number'); //class or id name all controls to need format
  4.         arrNumber = new Array();
  5.       for(var i = 0; i< arr.length;i++){
  6.         arrNumber[i] = formatMoney($(arr[i]).text());
  7.         $(arr[i]).text(arrNumber[i]);
  8.       }
  9. })
  10.  
  11. function formatMoney(money) {
  12.     decPlaces = 0;
  13.     thouSeparator = ',';
  14.     decSeparator = '.';
  15.     var n = money,
  16.     decPlaces = isNaN(decPlaces = Math.abs(decPlaces)) ? 2 : decPlaces,
  17.     decSeparator = decSeparator == undefined ? "." : decSeparator,
  18.     thouSeparator = thouSeparator == undefined ? "," : thouSeparator,
  19.     sign = n < 0 ? "-" : "",
  20.     i = parseInt(n = Math.abs(+n || 0).toFixed(decPlaces)) + "",
  21.     j = (j = i.length) > 3 ? j % 3 : 0;
  22.     return sign + (j ? i.substr(0, j) + thouSeparator : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thouSeparator) + (decPlaces ? decSeparator + Math.abs(n - i).toFixed(decPlaces).slice(2) : "");
  23. };
Advertisement
Add Comment
Please, Sign In to add comment