JS去掉小数末尾多余的0,并且最多保留两位小数

"'' -> 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
      }
    }
  }

你可能感兴趣的:(JS去掉小数末尾多余的0,并且最多保留两位小数)