The solution to the problem of imprecision in toFixed

found a lot of methods on the Internet, all of which did not take into account the negative number. It was found that the negative number did not add 1, and the reliable toFixed rewriting method was found

.
Number.prototype.toFixed=function(s) {//toFixed
  return (parseInt(this * Math.pow( 10, s ) + 0.5)/ Math.pow( 10, s )).toString();
}

let num = -2999033.45645;
num.toFixed(4);
//-2999033.4564 1
Mar.10,2022

Number.prototype.toFixed=function(s) {//toFixed
  const adjust = this >= 0 ? 0.5 : -0.5;
  return (parseInt(this * Math.pow( 10, s ) + adjust)/ Math.pow( 10, s )).toString();
}
Menu