封装一个函数的方法,引入后使用
// 格式化金额
function formatMoney(n) {
const regex = /\d{1,3}(?=(\d{3})+(\.|$))/g // 替换规则
n = String(Math.round(n * Math.pow(10, 2))) // 乘100 四舍五入
let integer = n.substr(0, n.length - 2).replace(regex, '$&,') // 最后两位前的为整数
let decimal = n.substr(n.length - 2) // 最后两位为小数
const result = `${integer || 0}.${decimal}`
return result
}
或者使用js提供的方法 toLocaleString()
let num = '9584812'
console.log(parseInt(num).toLocaleString())
原文链接:https://blog.youkuaiyun.com/z291493823/article/details/121702399