重写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

你可能感兴趣的:(JavaScript,bug,javascript,前端)