JavaScript floor() 方法

定义和用法

floor() 方法可对一个数进行下舍入。

语法

Math.floor(x)
参数 描述
x 必需。任意数值或表达式。

返回值

小于等于 x,且与 x 最接近的整数。

说明

floor() 方法执行的是向下取整计算,它返回的是小于或等于函数参数,并且与之最接近的整数。

实例

In this example we will use the floor() method on different numbers:

<script type="text/javascript">

document.write(Math.floor(0.60) + "<br />")
document.write(Math.floor(0.40) + "<br />")
document.write(Math.floor(5) + "<br />")
document.write(Math.floor(5.1) + "<br />")
document.write(Math.floor(-5.1) + "<br />")
document.write(Math.floor(-5.9))

</script>

输出:

0
0
5
5
-6
-6

TIY

floor()
如何使用 floor() 方法。