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

这篇文章主要介绍了Math.floor 方法用于对数值向下取整,即得到小于或等于该数值的最大整数,需要的朋友可以参考下

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 方法》一文中的相关描述。

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wgxgww.html