Rounding issue in Math.round() & .toFixed()

前端 未结 7 1117
半阙折子戏
半阙折子戏 2020-12-10 16:58

I used below two methods :

Number.prototype.myRound = function (decimalPlaces) {
    var multiplier = Math.pow(10, decimalPlaces);

    return (Math.round(th         


        
7条回答
  •  半阙折子戏
    2020-12-10 17:38

    The problem is probably floating point inaccuracy, thus you might get different results in different cases (different gathering of a number, different browsers etc.).

    See also this: toFixed(2) rounds "x.525" inconsistently?

提交回复
热议问题