const toCustomerFixed = (num, decimal) => {
num = num.toString();
let index = num.indexOf(".");
if (index !== -1) {
num = num.substring(0, decimal + index + 1);
} else {
num = num.substring(0);
}
return formatWithRegex(parseFloat(num).toFixed(decimal));
};
const formatWithRegex = (number) => {
return !(number + "").includes(".")
? // 就是说1-3位后面一定要匹配3位
(number + "").replace(/\d{1,3}(?=(\d{3})+$)/g, (match) => {
return match + ",";
})
: (number + "").replace(/\d{1,3}(?=(\d{3})+(\.))/g, (match) => {
return match + ",";
});
};
JS保留小数点后几位不做四舍五入,设置千分位
最新推荐文章于 2024-07-22 08:55:10 发布