欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

JavaScript floor() 方法

JavaScript Math 对象

定义和用法

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

语法

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

返回值

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

说明

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

实例

在本例中,我们将在不同的数字上使用 floor() 方法:

<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() 方法。
JavaScript Math 对象

来自 http://www.w3school.com.cn/jsref/jsref_floor.asp
普通分类: