可靠的JS舍入数字与3个十进制数的toFixed(2)

jQu*_*ast 5 javascript rounding tofixed

我只是想整理一下1.275.toFixed(2),我期待的回报是1.28而不是1.27.

使用各种计算器和舍入​​到最接近的百分之一的简单方法,如果最后一个数字大于或等于五,它应该向上舍入.

如果这不能与toFixed(2)一起使用,那怎么办?

人们询问console.log(1.275.toFixed(2))是否打印掉1.28,这是一个快速截图MacOS Chrome版本55.0.2883.95(64位)

在此输入图像描述

Rob*_*sen 5

toFixed()方法在四舍五入中是不可靠的(参见ÁlvaroGonzález关于为什么会出现这种情况的答案).

在当前的Chrome和Firefox中,调用会toFixed()产生以下不一致的结果:

35.655.toFixed(2) // Yields "36.66" (correct)
35.855.toFixed(2) // Yields "35.85" (wrong, should be "35.86")
Run Code Online (Sandbox Code Playgroud)

MDN描述了可靠的舍入实现:

// Closure
(function() {
  /**
   * Decimal adjustment of a number.
   *
   * @param {String}  type  The type of adjustment.
   * @param {Number}  value The number.
   * @param {Integer} exp   The exponent (the 10 logarithm of the adjustment base).
   * @returns {Number} The adjusted value.
   */
  function decimalAdjust(type, value, exp) {
    // If the exp is undefined or zero...
    if (typeof exp === 'undefined' || +exp === 0) {
      return Math[type](value);
    }
    value = +value;
    exp = +exp;
    // If the value is not a number or the exp is not an integer...
    if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0)) {
      return NaN;
    }
    // Shift
    value = value.toString().split('e');
    value = Math[type](+(value[0] + 'e' + (value[1] ? (+value[1] - exp) : -exp)));
    // Shift back
    value = value.toString().split('e');
    return +(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp));
  }

  // Decimal round
  if (!Math.round10) {
    Math.round10 = function(value, exp) {
      return decimalAdjust('round', value, exp);
    };
  }
  // Decimal floor
  if (!Math.floor10) {
    Math.floor10 = function(value, exp) {
      return decimalAdjust('floor', value, exp);
    };
  }
  // Decimal ceil
  if (!Math.ceil10) {
    Math.ceil10 = function(value, exp) {
      return decimalAdjust('ceil', value, exp);
    };
  }
})();

// Round
Math.round10(55.55, -1);   // 55.6
Math.round10(55.549, -1);  // 55.5
Math.round10(55, 1);       // 60
Math.round10(54.9, 1);     // 50
Math.round10(-55.55, -1);  // -55.5
Math.round10(-55.551, -1); // -55.6
Math.round10(-55, 1);      // -50
Math.round10(-55.1, 1);    // -60
Math.round10(1.005, -2);   // 1.01 -- compare this with Math.round(1.005*100)/100 above
// Floor
Math.floor10(55.59, -1);   // 55.5
Math.floor10(59, 1);       // 50
Math.floor10(-55.51, -1);  // -55.6
Math.floor10(-51, 1);      // -60
// Ceil
Math.ceil10(55.51, -1);    // 55.6
Math.ceil10(51, 1);        // 60
Math.ceil10(-55.59, -1);   // -55.5
Math.ceil10(-59, 1);       // -50
Run Code Online (Sandbox Code Playgroud)


Álv*_*lez 5

1.275底座10具有数目有限位数,但是当转换为基体2变为周期性的:

= 0b1.01000110011001100110011001100110011001100110011010
         ^^^^
Run Code Online (Sandbox Code Playgroud)

由于它具有无限数字,因此除非您使用任意精度库(将数字表示为文本以将它们保留在基数10中),否则无法在计算机中精确表示它.出于性能原因,JavaScript编号不使用此类库.

由于原始值在到达JavaScript时已经失去了精度,因此舍入它不会改善它.