JS toFixed()方法精度丢失解决方法

发布时间 2023-11-17 11:07:37作者: 波特卡斯D

JS toFixed()方法精度丢失

toFixed()方法可把Number四舍五入为指定小数位数的数字。但这个方法并不完全遵从四舍五入的规则,如
2.485.toFixed(2) //=>2.48

网上流行的说法是遵循银行家舍入法,即:四舍六入五成双。四舍六入字面意思,5的话看后面不为零就进一,如果为零看5前面的数是奇数还是偶数,奇数进一偶数舍弃。但经过实际测试发现也不完全遵从这个规则(chrome下),如

2.845.toFixed(2) //=>2.48
1.485.toFixed(2) //=>2.49

解决方法,在main入口(如App.vue),重写toFixed方法,分几种情况来处理

Number.prototype.toFixed = function (n) {
  if (n > 20 || n < 0) {  //精度允许0-20
    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;
};