千分位

export function fmt(value, digit, thousand = true) {
if (!validNumber(value)) return '-';

value = (Number(value) || 0).toFixed(digit);

//加千分符
if (thousand && value.indexOf('e') < 0) {//科学计数法不需要加千分符
//取符号
let sign = value[0] === '-' ? '-' : '';
if (sign) value = value.slice(1);

//小数点位置
let point = value.indexOf('.');

//没有小数点,小数点就在最后一位
if (point === -1) point = value.length;

//整数部分
let big = value.slice(0, point).split('');

let res = value.slice(point);//取小数部分
for (let i = 0; i < big.length; i++) {
res = big[big.length - i - 1] + res;//从个位开始往前累加
if (i % 3 === 2 && i < big.length - 1)//每三位加一个逗号,最后一位不加
res = ',' + res;
}

value = sign + res;//拼上符号
}

return value;
}
原文地址:https://www.cnblogs.com/guidan/p/10566610.html