js添加千分位分割符
function toThousands(num, n = 2) {
if (!num || Number.isNaN(num)) {
num = 0;
}
let newNum = Number(num);
let re = /\B(?=(\d{3})+(?!\d))/g;
let s = 10 ** n;
return (Math.round(newNum * s) / s)
.toFixed(n)
.replace(re, ",");
}
js删除千分位分割符
function delThousands(num) {
num = String(num).replace(/,/g, '');
if (num.indexOf(".") > 0) {
num = num.replace(/0+?$/, "");//去除尾部多余的0
num = num.replace(/[.]$/, "");//如果最后一位是.则去掉
}
if (num == 0 || num == 'NaN') {
num = ''
}
return num;
}