?重点如下
function splitStr(num) {
// 转化成字符串
let numStr = `${num}`
let isNegative = false
if (numStr.startsWith('-')) {
isNegative = true
numStr = numStr.slice(1)
}
const parts = numStr.split('.')
let integerPart = parts[0]
let decimalPart = parts[1] || ''
integerPart = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, ',')
let result = integerPart
if (decimalPart) {
result = integerPart + '.' + decimalPart
}
if (isNegative) {
result = `-` + result
}
return result
}
const num = 12323.333
const reuslt = splitStr(num)
console.log(reuslt)