数字保留两位小数,并千分位标识 - davy-gan/web GitHub Wiki
function formatMoney(num, unit = '元') {
if (num === '') {
return;
} else if (isNaN(num)) {
return num;
}
return `${parseFloat(num).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,')}${unit}`;
}
formatNum (num) {
if (num === '') {
return;
} else if (isNaN(num)) {
return num;
}
return (num + '').replace(/(\d{1,3})(?=(\d{3})+(?:$|\.))/g, '$1,');
}
console.log(String(12345678.321).replace(/\d{1,3}(?=(\d{3})+(\.|$))/g,'$&,'))