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

转载于:https://blog.51cto.com/14072794/2325606

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值