JS去掉小数末尾多余的0,并且最多保留两位小数
2022-11-08 本文已影响0人
宏_4491
"'' -> 0
0.00 -> 0
1 -> 1
1.10 -> 1
1.213000 -> 1.21
1.01 -> 1.01
function unifyNumber(num) {
if (num === '') {
return 0
} else {
let handleNum = parseFloat(num)
let isToFixed = handleNum.toString().includes('.') && handleNum.toString().split('.')[1].length > 2
if (isToFixed) {
return handleNum.toFixed(2)
} else {
return handleNum
}
}
}