/**
* 千位符格式化数值
* @param num 数值
* @param dec 小数位数,默认为2位小数
* @returns xx,xxx.xx
*/
export const formatMoney = (num, dec = 2) => {
if (typeof num !== 'number') {
num = Number(num);
if (Number.isNaN(num)) {
num = 0;
}
}
let str = num.toFixed(dec);
if (str.indexOf('.') === -1) {
str += '.'
}
return str.replace(/(\d)(?=(\d{3})+\.)/g, '$1,').replace(/\.$/, '');
}
千位符格式化数值
于 2022-07-12 15:37:35 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)