我有一个除法的结果,我希望放弃结果数的小数部分。
我该怎么做呢?
我有一个除法的结果,我希望放弃结果数的小数部分。
我该怎么做呢?
当前回答
使用Math.round()。
(亚历克斯的回答更好;我做了一个假设:)
其他回答
在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和其他现代浏览器中都可以运行。
你也可以
parseInt(a/b)
对于ES6实现,使用如下代码:
const millisToMinutesAndSeconds = (millis) => {
const minutes = Math.floor(millis / 60000);
const seconds = ((millis % 60000) / 1000).toFixed(0);
return `${minutes}:${seconds < 10 ? '0' : ''}${seconds}`;
}
使用Math.round()函数。
Math.round(65.98) // will return 66
Math.round(65.28) // will return 65
Math.trunc()和~~去掉小数部分而不影响整数部分。
例如:
console.log(Math.trunc(3.9)) // 3
console.log(~~(3.9)) // 3