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

我该怎么做呢?


当前回答

固定的会像圆的一样。

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

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

其他回答

例如:

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}`;
}

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

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

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