toFixed() method bug
There is a bug in the native method toFixed() of the Number object in js. Sometimes the decimal cannot be correctly rounded and outputted. You can use the following method instead:Number.prototype.ToFixed = function (n) {
const factor = Math.pow(10, n);
const roundedValue = Math.round(this * factor) / factor;
return roundedValue.toFixed(n);
};
const num = 3.14159;
console.log(num.toFixedCustom(2)); // 输出 3.14
console.log(num.toFixedCustom(3)); // 输出 3.142