共计 305 个字符,预计需要花费 1 分钟才能阅读完成。
function formatNumber(val){if (val === undefined){return '暂无数据'} else if (isNaN(+val)){return val} else if (!val){return 0}
let newVal = val.toString()
let arr = newVal.split('.')
let inpart = arr[0].replace(/(\d)(?=(?:\d{3})+$)/g,'$1,')
if (arr[1]){return inpart + '.' + arr[1]
}else {return inpart}
}
console.log(formatNumber(12345678));
后果:
正文完