toFixed(2) rounds “x.525” inconsistently?

前端 未结 4 467
半阙折子戏
半阙折子戏 2020-12-06 13:20

I\'m experiencing rounding errors when using toFixed:

I used toFixed(2) on my numeric value calculations, but the rounding results are not as expected

4条回答
  •  感动是毒
    2020-12-06 14:21

    Convert the number to String and work with it?

    That is the last resort after I have tried to use Math.round, or simulate the nearest rounding with Math.ceil, but failed. When multiplying with 100, some number (such as 17.525) will be a bit less than 100 times its value (1752.5), while other numbers (such as 17.545) will be a bit more than 100 times its value (1754.5).

提交回复
热议问题