js保留小数点后一位数字(不四舍五入)

保留小数点后一位数字

   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

猜你喜欢

转载自blog.csdn.net/Maxueyingying/article/details/135224087