JS多位数数字进行千位符格式化代码,带小数位。常用于金额
function thousandNumFormat(num){
let str = num.toString().replace(/,/g,"");
let integer = str.split(".")[0]; //获取整数位
let decimal = str.split(".")[1]; //获取小数
let res = [];
if(integer.length > 3){
let counter = 0;
for (let i = integer.length - 1; i >= 0; i--) {
counter++;
res.unshift(integer[i]);
if (!(counter % 3) && i != 0) {
res.unshift(',');
}
}
if(decimal){
res = res.join("") + '.' + decimal;
}else{
res = res.join("");
}
}else{
res = str;
}
return res;
}