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

我该怎么做呢?


当前回答

如果不关心舍入,只需将数字转换为字符串,然后删除句号之后的所有内容,包括句号。不管有没有小数点,这个都成立。

const sEpoch = ((+new Date()) / 1000).toString();
const formattedEpoch = sEpoch.split('.')[0];

其他回答

在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和其他现代浏览器中都可以运行。

固定的会像圆的一样。

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

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

使用Math.round()。

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

例如:

var x = 9.656;
x.toFixed(0);           // returns 10
x.toFixed(2);           // returns 9.66
x.toFixed(4);           // returns 9.6560
x.toFixed(6);           // returns 9.656000 

or

parseInt("10");         // returns 10
parseInt("10.33");      // returns 10
parseInt("10 20 30");   // returns 10
parseInt("10 years");   // returns 10
parseInt("years 10");   // returns NaN  

对于ES6实现,使用如下代码:

const millisToMinutesAndSeconds = (millis) => {
  const minutes = Math.floor(millis / 60000);
  const seconds = ((millis % 60000) / 1000).toFixed(0);
  return `${minutes}:${seconds < 10 ? '0' : ''}${seconds}`;
}