trollolodn icon

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

trollolodn | PRO | 04/18/13 05:10:37 AM UTC | 0 ⭐ | 160 👁️ | Never ⏰ | []
JavaScript |

980 B

|

None

|

0 👍

/

0 👎

//format number for all controls html of the same class or id
$(function(){
    arr = $('.col_3.number'); //class or id name all controls to need format
        arrNumber = new Array();
      for(var i = 0; i< arr.length;i++){
        arrNumber[i] = formatMoney($(arr[i]).text());
        $(arr[i]).text(arrNumber[i]);
      }
})
 
function formatMoney(money) {
    decPlaces = 0;
    thouSeparator = ',';
    decSeparator = '.';
    var n = money,
    decPlaces = isNaN(decPlaces = Math.abs(decPlaces)) ? 2 : decPlaces,
    decSeparator = decSeparator == undefined ? "." : decSeparator,
    thouSeparator = thouSeparator == undefined ? "," : thouSeparator,
    sign = n < 0 ? "-" : "",
    i = parseInt(n = Math.abs(+n || 0).toFixed(decPlaces)) + "",
    j = (j = i.length) > 3 ? j % 3 : 0;
    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) : "");
};

Comments