js thousand separator and change td content

js thousand seprator and change TD content

// integer 
function addCommas(n){
    var rx=  /(\d+)(\d{3})/;
    return String(n).replace(/^\d+/, function(w){
        while(rx.test(w)){
            w= w.replace(rx, '$1,$2');
        }
        return w;
    });
}

//decimal
function thousandBitSeparator(num) {
        return num && num
            .toString()
            .replace(/(\d)(?=(\d{3})+\.)/g, function($0, $1) {
                return $1 + ",";
            });
}

// change table td content by getElementsByClassName()

function changeTDcontent(){
    var olist=document.getElementsByClassName("cl_id");
    for(var i=0;i<olist.length;i++){
        var c=olist[i].innerHTML;
        olist[i].innerHTML=addCommas(c);
    }
}

 

posted on 2017-11-02 20:07  rojas  阅读(236)  评论(0编辑  收藏  举报