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

前端 未结 7 1098
半阙折子戏
半阙折子戏 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:32

    In my software I use this:

    (require DecimalJS)

    Number.prototype.toFixed = function(fixed) {
        return (new Decimal(Number(this))).toFixed(parseFloat(fixed) || 
    0);
    };
    
    
    var x = 1.005;
    console.log( x.toFixed(2) ); //1.01
    

提交回复
热议问题