js retains one digit after the decimal point (no rounding)

Keep one digit after the decimal point

   let num='22.256'
   let numNew=  this.formattedNumber(num)
   formattedNumber(num) {
    
    
            const numStr = num.toString();
            const decimalIndex = numStr.indexOf('.');
            if (decimalIndex !== -1) {
    
    
                // 截取小数点后一位 
                return numStr.slice(0, decimalIndex + 2);
            } else {
    
     // 如果是整数,直接返回 
                return numStr;
            }
   }
    console.log(numNew)    //22.2

Guess you like

Origin blog.csdn.net/Maxueyingying/article/details/135224087