JavaScript Math.floor方法(对数值向下取整)_技术学院_宜昌市隼壹珍商贸有限公司

您好,欢迎访问宜昌市隼壹珍商贸有限公司

400 890 5375
当前位置: 主页 > 新闻动态 > 技术学院

JavaScript Math.floor方法(对数值向下取整)

发布时间:2026-01-18  |  点击率:

JavaScript Math.floor 方法
Math.floor 方法用于对数值向下取整,即得到小于或等于该数值的最大整数。语法如下:

Math.floor(x)
参数说明:

参数 说明
x 必需。必须是一个数值。

提示:该方法与 Math.ceil 方法正好相反。

Math.floor 方法实例

<script language="JavaScript">
document.write( Math.floor(0.35) + "<br />" );
document.write( Math.floor(10) + "<br />" );
document.write( Math.floor(-10) + "<br />" );
document.write( Math.floor(-10.1) );
</script>

运行该例子,输出:

0
10
-10
-11

Math.floor 可能不准的问题

如果参数 x 是一个涉及浮点数的表达式,那么由于计算机的固有原理,可能导致表达式应用 Math.floor 方法后结果不准确(不符合常理),具体参考《Math.ceil 方法》一文中的相关描述。

全国统一服务电话

400 890 5375

电子邮箱:879577@qq.com

公司地址:宜昌市西陵区黄河路5号三峡明珠10栋1051室

咨询微信

TEL:13680874598