1、金额进行千分
function commafy(num) {
if ((num + "").trim() == "") {
return "";
}
if (isNaN(num)) {
return "";
}
num = num + "";
if (/^.*\..*$/.test(num)) {
const pointIndex = num.lastIndexOf(".");
const intPart = num.substring(0, pointIndex);
const pointPart = num.substring(pointIndex + 1, num.length);
intPart = intPart + "";
const re = /(-?\d+)(\d{3})/
while (re.test(intPart)) {
intPart = intPart.replace(re, "$1,$2")
}
num = intPart + "." + pointPart;
} else {
num = num + "";
const re = /(-?\d+)(\d{3})/
while (re.test(num)) {
num = num.replace(re, "$1,$2")
}
}
return num;
}
2、toFixed()保留两位小数丢失精度解决办法
function toFixedTwo(num) {
return (num*100).toFixed(0)/100
}