JavaScript Math floor()

Math.floor() 函数将数字向下舍入到下一个最小的整数。

示例

let number = 38.8;

// round number to nearest smallest number let roundedNumber = Math.floor(number);
console.log(roundedNumber); // Output: 38

Math.floor() 语法

Math.floor() 函数的语法是:

Math.floor(x)

floor() 是一个静态方法,通过 Math 类名进行调用。


Math.floor() 参数

Math.floor() 函数接受:

  • x - 一个数字

Math.floor() 返回值

  • 返回小于或等于给定数字的最大整数。
  • null 返回 0。

示例:使用 Math.floor()

// using Math.floor()

var num = Math.floor(1.8645);
console.log(num); // 1 var num = Math.floor(-0.456); console.log(num); // -1
var num = Math.floor("4");
console.log(num); // 4
// Returns 0 for null var num = Math.floor(null);
console.log(num); // 0
// Returns NaN for non-numeric types var num = Math.floor("JavaScript");
console.log(num); // NaN var num = Math.floor(NaN); console.log(num); // NaN

输出

1
-1
4
0
NaN
NaN

注意: Math.floor()null 返回 0,而不是 NaN


另请阅读

你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战