<script>
var data = 42542335434.45334;
//使用正则替换,每隔三个数加一个','
var b = data .toFixed(2).replace(/(d)(?=(d{3})+.)/g, '$1,');
</script>
(不处理小数点)方法二:
function thousandBitSeparator(num) {
return num && num.toString().replace(/(d)(?=(d{3})+.)/g, function($0, $1) {
return $1 + ",";
});
}
console.log(thousandBitSeparator(123456790.12));
https://www.jianshu.com/p/928c68f92c0c
function numFormat(num){
var res=num.toString().replace(/d+/, function(n){ // 先提取整数部分
return n.replace(/(d)(?=(d{3})+$)/g,function($1){
return $1+",";
});
})
return res;
}
console.log(numFormat(a)); // "1,234,567,894,532"
console.log(numFormat(b)); // "673,439.4542"