JavaScript中常用的Math方法
作用:用来返回参数的绝对值
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) //输出4Math.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) //输出4Math.pow(3,4) //输出81
7.Math.sqrt()
作用:返回参数值的平方根,如果参数是一个负值,则返回NaN
Math.sqrt(4) //输出2 Math.sqrt(-81) //输出NaN
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。