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

我该怎么做呢?


当前回答

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

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

其他回答

例如:

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  

你也可以

parseInt(a/b)

您可以使用. tofixed(0)来删除完整的小数部分或在参数中提供您希望小数被截断的数字。

注意:toFixed将数字转换为字符串。

对于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