数字转成成千位表示法
// 格式化数字成千分位表示 例如1000=》1,000
// 10000 ==> 10,000
export const formatToThousands = (n: any) => {
const num = n.toString();
let decimals = '';
// 判断是否有小数
num.indexOf('.') > -1 ? decimals = num.split('.')[1] :decimals=decimals;
const len = num.length;
if (len <= 3) {
return num
} else {
let temp = '';
const remainder = len % 3;
decimals ? temp = '.' + decimals : temp=temp;
if (remainder > 0) { // 不是3的整数倍
return num.slice(0, remainder) + ',' + num.slice(remainder, len).match(/\d{3}/g).join(',') + temp
} else { // 是3的整数倍
return num.slice(0, len).match(/\d{3}/g).join(',') + temp
}
}
};