Summary of H5 numbers retaining decimals and removing the last decimal place of 0

Summary of H5 numbers retaining decimals and removing the last decimal place of 0

1. Demand background
  • In the recent development of the H5 project, since the number retains decimals, no matter whether the last digit of the decimal is greater than 0, there will be 0. For example: 10 will become 10.00 after retaining two decimal places. In some scenarios, the 0 after the decimal is not required.
solution
  • // 方法如下:
    
     function zeroHandle(str) {
          
          
        if (str.length && str[str.length - 1] !== '.' && str[str.length - 1] === '0') {
          
          
          return zeroHandle(str.substring(0, str.length - 1))
        } else if (str.length && str[str.length - 1] === '.') {
          
          
          return str.substring(0, str.length - 1)
        } return str
     }
    
     zeroHandle(typeof value === 'string'? value : typeof value === 'number' ? value.toString() : '')
    
    
  • solve.

Guess you like

Origin blog.csdn.net/qq_34917408/article/details/127730572