将数字加上千分符
// 转换千分符
function thousands(str) {
//将所传入的数字或其他格式的字符转为字符串。
str=str+’’;
//str 字符类型的数字
let thousandsReg = /(\d)(?=(\d{3})+$)/g;
const numArr = str.split(".");
numArr[0] = numArr[0].replace(thousandsReg, “$1,”);
return numArr.join(".");
}
//使用方法
thousands(1000); //1,000
thousands(1300.00); //1,300.00
thousands(1135200); //1,135,200
附上代码
// 转换千分符
function thousands(str) {
//将所传入的数字或其他格式的字符转为字符串。
str=str+'';
//str 字符类型的数字
let thousandsReg = /(\d)(?=(\d{3})+$)/g;
const numArr = str.split(".");
numArr[0] = numArr[0].replace(thousandsReg, "$1,");
return numArr.join(".");
}
//使用方法
thousands(1000); //1,000
thousands(1300.00); //1,300.00
thousands(1135200); //1,135,200