为什么 Math.round() 在值为 0.5 时不四舍五入?

Why is Math.round() not rounding up when the value is 0.5?

目前有一个基本如下所示的公式:

Math.round((20.49-14.99)*5);

结果数字是 27.5 和 AFAIK,Math.round() 如果数字是 0.5 及以上则向上舍入,如果低于则向下舍入。除非我误解了,否则我假设结果将四舍五入为 28(例如在 Microsoft Excel 中),但相反,它向下四舍五入为 27.

一开始我以为是我的代码有什么问题,但是当我用console logging的时候,结果一模一样。

我是否遗漏了什么?如果遗漏了,我该如何从 0.5 开始进行汇总?

由于浮点数的工作方式,(20.49-14.99)*5 的结果实际上是 27.499999999999993,比 28

更接近 27

(20.49-14.99)*527.499999999999993,因此 JavaScript 会将其四舍五入为更接近的值。您应该使用 Math.ceil 舍入值