JavaScript的toFixed函数

14

预期结果是:

(1.175).toFixed(2) = 1.18 and
(5.175).toFixed(2) = 5.18

但在 JavaScript 中显示:

(1.175).toFixed(2) = 1.18 but 
*(5.175).toFixed(2) = 5.17*

如何纠正这个问题?


为什么这是个问题? - Denys Séguret
我不知道这是否能解决你的问题:https://dev59.com/EWgv5IYBdhLWcg3wKtlA - Vinicius Lima
(5.175).toFixed(2)的预期结果是什么? - Sushovan Mukherjee
3
可能是重复的吗?https://dev59.com/A3NA5IYBdhLWcg3wZ89S - Vikram Deshmukh
这样一个函数的行为真是浪费时间。 - user3808307
6个回答

11

这不是一个bug。它与数字不是以十进制而是以IEEE754的格式存储有关(因此5.175没有被完全精确存储)。

如果您想向特定方向(向上)舍入,并且您始终具有此精度的数字,则可以使用以下技巧:

(5.175 + 0.00001).toFixed(2)

7

您可以尝试使用round而不是toFixed。

Math.round(5.175*100)/100

如果您想要,甚至可以尝试将其放入某个原型方法中。

创建了一个jsBin,实现了Number的简单原型。

Number.prototype.toFixed = function(decimals) {
 return Math.round(this * Math.pow(10, decimals)) / (Math.pow(10, decimals)); 
};

1
我喜欢这个解决方案,除了你打败了函数的目的,即确保小数点后有特定数量的数字。你的函数在像1、1.0、1.00000等数字上失败了。你可以通过在返回语句中添加"toFixed()"来解决这个问题,例如:"return (Math.round(this * Math.pow(10, decimals)) / (Math.pow(10, decimals))).toFixed(decimals);" - stldoug
我喜欢你的评论,因为我在使用这个解决方案时也遇到了同样的问题。然而,你的方法存在一个问题,就是在定义函数时不能使用正在重新定义的那个函数。它会进入递归的乱七八糟的地方。我在这里提供了一个解决方案https://jsfiddle.net/yxjd78sj/,它捕获了旧的.toFixed()函数定义,并将其作为.oldToFixed()来使用。请告诉我你的想法! - 3abqari

2

这是因为数字以IEEE754格式存储。

我建议您使用Math类(round,floor或ceil方法,具体取决于您的需求)。

我刚刚创建了一个名为MathHelper的类,可以轻松解决您的问题:

var MathHelper = (function () {
    this.round = function (number, numberOfDecimals) {
        var aux = Math.pow(10, numberOfDecimals);
        return Math.round(number * aux) / aux;
    };
    this.floor = function (number, numberOfDecimals) {
        var aux = Math.pow(10, numberOfDecimals);
        return Math.floor(number * aux) / aux;
    };
    this.ceil = function (number, numberOfDecimals) {
        var aux = Math.pow(10, numberOfDecimals);
        return Math.ceil(number * aux) / aux;
    };

    return {
        round: round,
        floor: floor,
        ceil: ceil
    }
})();

使用方法:

MathHelper.round(5.175, 2)

示例: http://jsfiddle.net/v2Dj7/

无法解决浮点数精度问题的通用方法:MathHelper.round(1.005, 2)返回 1,但期望返回 1.01(1.005).toFixed(2)同样如此。 - ujay68

0
obj = {
    round(val) {
      const delta = 0.00001
      let num = val
      if (num - Math.floor(num) === 0.5) {
        num += delta
      }
      return Math.round(num + delta)
    },
  fixed(val, count = 0) {
      const power = Math.pow(10, count)
      let res = this.round(val * power) / power
      let arr = `${res}`.split('.')
      let addZero = ''
      for (let i = 0; i < count; i++) {
        addZero += '0'
      }
      if (count > 0) {
        arr[1] = ((arr[1] || '') + addZero).substr(0, count)
      }
      return arr.join('.')
  }
}
obj.fixed(5.175, 2)

// "5.18"


0
Kippie 你的解决方案有问题 其中之一
39133.005.toFixed(2) => 39133 

var Calc = function () {
    var self = this;

this.float2Array = function(num) {
    var floatString = num.toString(),
        exp = floatString.indexOf(".") - (floatString.length - 1),
        mant = floatString.replace(".", "").split("").map(function (i) { return parseInt(i); });
    return { exp: exp, mant: mant };
};

this.round2 = function (num, dec, sep) {
    var decimal = !!dec ? dec : 2,
    separator = !!sep ? sep : '',
    r = parseFloat(num),
    exp10 = Math.pow(10, decimal);
    r = Math.round(r * exp10) / exp10;

    var rr = Number(r).toFixed(decimal).toString().split('.');

    var b = rr[0].replace(/(\d{1,3}(?=(\d{3})+(?:\.\d|\b)))/g, "\$1" + separator);
    r = (rr[1] ? b + '.' + rr[1] : b);

    return r;
};

this.toFixed10 = function (f, num) {
    var prepareInt = self.float2Array(f),
        naturalInt = prepareInt.mant,
        places = Math.abs(prepareInt.exp),
        result = prepareInt.mant.slice(),
        resultFixedLenth;

    // if number non fractional or has zero fractional part
    if (f.isInt()) {
        return f.toFixed(num);
    }
    // if the number of decimal places equals to required rounding
    if (places === num) {
        return Number(f).toString();
    }
    //if number has trailing zero (when casting to string type float 1.0050 => "1.005" => 005 <0050)
    if (places < num) {
        return Number(f).round2(num);
    }

    for (var e = naturalInt.length - (places > num ? (places - num) : 0), s = 0; e >= s; e--) {
        if (naturalInt.hasOwnProperty(e)) {
            if (naturalInt[e] > 4 && naturalInt[e - 1] < 9) {
                result[e] = 0;
                result[e - 1] = naturalInt[e - 1] + 1;
                break;
            } else if (naturalInt[e] > 4 && naturalInt[e - 1] === 9) {
                result[e] = 0;
                result[e - 1] = 0;
                result[e - 2] = naturalInt[e - 2] < 9 ? naturalInt[e - 2] + 1 : 0;
            } else if (e === 0 && naturalInt[e] === 9 && naturalInt[e + 1] === 9) {
                result[e] = 0;
                result.unshift(1);
            } else {
                break;
            }
        }
    }

    if (places - num > 0) {
        resultFixedLenth = result.slice(0, -(places - num));
    } else {
        for (var i = 0, l = num - places; i < l; i++) {
            result.push(0);
        }
        resultFixedLenth = result;
    }

    return (parseInt(resultFixedLenth.join("")) / Math.pow(10, num)).round2(num);
};
this.polyfill = function() {
    if (!Array.prototype.map) {
        Array.prototype.map = function (callback, thisArg) {
            var T, A, k;
            if (this == null) { throw new TypeError(' this is null or not defined'); }
            var O = Object(this), len = O.length >>> 0;
            if (typeof callback !== 'function') { throw new TypeError(callback + ' is not a function'); }
            if (arguments.length > 1) { T = thisArg; }

            A = new Array(len);
            k = 0;
            while (k < len) {
                var kValue, mappedValue;
                if (k in O) {
                    kValue = O[k];
                    mappedValue = callback.call(T, kValue, k, O);
                    A[k] = mappedValue;
                }
                k++;
            }
            return A;
        };
    }
};

this.init = function () {
    self.polyfill();
    Number.prototype.toFixed10 = function (decimal) {
        return calc.toFixed10(this, decimal);
    }
    Number.prototype.round2 = function (decimal) {
        return calc.round2(this, decimal);
    }
    Number.prototype.isInt = function () {
        return (Math.round(this) == this);
    }
}
}, calc = new Calc(); calc.init();

这个很好用)


0

实际上,我认为这是Number.prototype.toFixed实现中的一个错误。在ECMA-262 20.1.3.3 10-a中给出的算法说要向上舍入作为决胜者。正如其他人所提到的,由于实现中的浮点不精确性,可能没有需要决胜的平局。但这并不代表它是正确的 :)

至少这种行为在FF、Chrome、Opera和Safari中是一致的。(还没有尝试其他浏览器。)

顺便说一句,你可以按照JS规范实现自己的toFixed版本,并且它的行为与你期望的一样。请参见http://jsfiddle.net/joenudell/7qahrb6d/


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接