重写toFixed方法,纠正四舍五入不正确的bug

const newToFixed=(value,length)=>{  
    let tempNum = 0;  
    let s,temp;  
    let s1 = value + "";  
    let start = s1.indexOf(".");  
    if(s1.substr(start+length+1,1)>=5){
        tempNum=1;  
    }
    temp = Math.pow(10,length);  
    s = Math.floor(value * temp) + tempNum;  
    return s/temp;  
}
 
console.log(newToFixed(1.115,2))    //1.12

猜你喜欢

转载自blog.csdn.net/m0_57033755/article/details/132849335