我有一个除法的结果,我希望放弃结果数的小数部分。

我该怎么做呢?


当前回答

U还可以显示小数点后(这里是2位数)的特定数字,使用以下代码:

var num = (15.46974).toFixed(2) 控制台.log(数字) // 15.47 console.log(类型编号) // 字符串

其他回答

你也可以

parseInt(a/b)

使用Math.round()函数。

Math.round(65.98) // will return 66 
Math.round(65.28) // will return 65

使用Math.round()。

(亚历克斯的回答更好;我做了一个假设:)

固定的会像圆的一样。

对于类似地板的行为使用%:

var num = 3.834234;
var floored_num = num - (num % 1); // floored_num will be 3

在ES2015中,Math.trunc()是可用的。

Math.trunc(2.3)                       // 2
Math.trunc(-2.3)                      // -2
Math.trunc(22222222222222222222222.3) // 2.2222222222222223e+22
Math.trunc("2.3")                     // 2
Math.trunc("two")                     // NaN
Math.trunc(NaN)                       // NaN

IE11及以下版本不支持,但在Edge和其他现代浏览器中都可以运行。