对于float的数据,toFIxed方法四舍五入不精确。浏览器和方法都是完全一样,仍然出现了误差。
查询了一些资料说使用的是银行家算法,
它是一个四舍六入五成双的诡异的方法(也叫银行家算法),"四舍六入五成双"含义:对于位数很多的近似数,当有效位数确定后,其后面多余的数字应该舍去,只保留有效数字最末一位,这种修约(舍入)规则是“四舍六入五成双”,也即“4舍6入5凑偶”这里“四”是指≤4 时舍去,"六"是指≥6时进上,"五"指的是根据5后面的数字来定,当5后有数时,舍5入1;当5后无有效数字时,需要分两种情况来讲:①5前为奇数,舍5入1;②5前为偶数,舍5不进。(0是偶数)。
但是对应chrome浏览器结果还是不对。IE浏览器测试数据正确
最后重写了toFIxed的原型方法
Number.prototype.toFixed = function (n) {
if (n > 20 || n < 0) {
throw new RangeError('toFixed() digits argument must be between 0 and 20');
}
const number = this;
if (isNaN(number) || number >= Math.pow(10, 21)) {
return number.toString();
}
if (typeof (n) == 'undefined' || n == 0) {
return (Math.round(number)).toString();
}
//判断是否为负数
var isMinus = number > 0 ? false : true;
let result = number.toString();
const arr = result.split('.');
// 整数的情况
if (arr.length < 2) {
result += '.';
for (let i = 0; i < n; i += 1) {
result += '0';
}
return result;
}
const integer = arr[0];
const decimal = arr[1];
//小数位数和精确位数相等时
if (decimal.length == n) {
return result;
}
//小数位数小于精确位数时
if (decimal.length < n) {
for (let i = 0; i < n - decimal.length; i += 1) {
result += '0';
}
return result;
}
result = integer + '.' + decimal.substr(0, n);
const last = decimal.substr(n, 1);
// 四舍五入,转换为整数再处理,避免浮点数精度的损失
if (parseInt(last, 10) >= 5) {
const x = Math.pow(10, n);
// 对于过精度位的下一位值大于5时,正数+1 负数-1.
// 正数例如6.257 转化为两位精度的小数是 6.26。
// 负数例如-6.258 转化为两位精度的小数是 -6.26。
result = (Math.round((parseFloat(result) * x)) + (isMinus ? -1 : 1)) / x;
result = result.toFixed(n);
}
return result;
};