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

我该怎么做呢?


当前回答

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

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

其他回答

你也可以

parseInt(a/b)

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

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

使用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

使用Math.round()。

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