重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

JavaScript中常用的Math方法

1.Math.abs()

作用:用来返回参数的绝对值

莲花网站制作公司哪家好,找创新互联!从网页设计、网站建设、微信开发、APP开发、成都响应式网站建设公司等网站项目制作,到程序开发,运营维护。创新互联成立与2013年到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联。

 Math.abs(-3);                       //输出3
 Math.abs(3);                        //输出3

2.Math.Max()

作用:返回参数中最大的那个值
Math.min()
作用:返回参数中最小的那个值

Math.max(2,-10,45)                //输出45

假若传的是一个数组,那需要借助apply来得到数组中的最大值或者最小值

var arr=[23,-9,67,34,-89,0];
Math.min.apply(null,arr);                               //输出-89

3.Math.floor()

作用:返回小于参数值的最大整数

 Math.floor(3.2)                                    //输出3
 Math.floor(-3.2)                                     //输出-4

4.Math.ceil()

作用:返回大于参数值的最小整数

Math.ceil(3.2)          //输出4
Math.ceil(-3.2)                      //输出-3

5.Math.round()

作用:四舍五入

 Math.round(0.1)                  //输出0
 Math.round(0.6)                    //输出1      

Math.round(-1.1)                  //输出-1  
Math.round(-1.5)                       //输出-1  
Math.round(-1.6)                     //输出-2  

6.Math.pow()

作用:返回以第一个参数为底数,第二个参数为幂的指数值

Math.pow(2,2)                       //输出4
Math.pow(3,4)          //输出81

7.Math.sqrt()

作用:返回参数值的平方根,如果参数是一个负值,则返回NaN

 Math.sqrt(4)                         //输出2
 Math.sqrt(-81)                     //输出NaN

当前名称:JavaScript中常用的Math方法
文章转载:http://cqcxhl.cn/article/gsjodi.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP