JavaScript中常用的Math方法

1.Math.abs()

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

 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

猜你喜欢

转载自blog.51cto.com/14072794/2325606