欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JavaScript中使用Math.floor()方法对数字取整

程序员文章站 2023-01-07 13:16:04
 此方法返回比最大的整数小于或等于参数 语法 math.floor( x ) ; 下面是参数的详细信息:  &nbs...

 此方法返回比最大的整数小于或等于参数
语法

math.floor( x ) ;

下面是参数的详细信息:

  •     x : 一个数字

返回值:

返回比最大的整数小于或等于一个数x
例子:

<html>
<head>
<title>javascript math floor() method</title>
</head>
<body>
<script type="text/javascript">

var value = math.floor(10.3);
document.write("first test value : " + value ); 
 
var value = math.floor(30.9);
document.write("<br />second test value : " + value ); 

var value = math.floor(-2.9);
document.write("<br />third test value : " + value ); 

var value = math.floor(-2.2);
document.write("<br />fourth test value : " + value ); 
</script>
</body>
</html>

这将产生以下结果:

first test value : 10
second test value : 30
third test value : -3
fourth test value : -3