X 归档 2020-04-01
In Javascript, if you need to round down a float, you can use Math.floor, but what if the number can be negative? Doing Math.floor on -2.5 will round down to -3.
Maybe, what you really want is Math.trunc, which will always just remove any decimal.
#100DaysOfCode #javascript
Maybe, what you really want is Math.trunc, which will always just remove any decimal.
#100DaysOfCode #javascript
在 JavaScript 里,如果你要把浮点数向下取整,可以用 Math.floor,但如果这个数可能是负数呢?对 -2.5 做 Math.floor 会向下舍入成 -3。
也许你真正想要的是 Math.trunc,它永远只是直接去掉小数部分。
#100DaysOfCode #javascript
也许你真正想要的是 Math.trunc,它永远只是直接去掉小数部分。
#100DaysOfCode #javascript

