js中常用的Math函数方法

 1.Math.abs():  返回绝对值

            console.log(Math.abs(-1));//1

            console.log(Math.abs('1.2'));//1.2,对于字符串会先转换成number类型    

 2.Math.ceil():  返回给定数值的最大整数,向上取整

            console.log(Math.ceil(5.1));//6

            console.log(Math.ceil(-5.1));//-5

            console.log(Math.ceil(-1.1));//-1

            console.log(Math.ceil(1.1));//2    

 3.Math.floor():  返回给定数值的最小整数,向下取整

            console.log(Math.floor(5.1));//5

            console.log(Math.floor(-5.1));//-6

            console.log(Math.floor(-1.1));//-2

            console.log(Math.floor(1.1));//1  

            console.log(Math.floor());//NaN

4.Math.min():  返回一组数据中的最小数据,如果没有参数,则结果为Infinity,如果有一项参数不能被转为数值,则结果为NaN

            console.log(Math.min(-1,-10,1,10));//-10

            console.log(Math.min());//Infinity

            console.log(Math.min('aa));//NaN

5.Math.max():  返回一组数据中的最大数据,如果没有参数,则结果为Infinity,如果有一项参数不能被转为数值,则结果为NaN

            console.log(Math.max(-1,-10,1,10));//10

            console.log(Math.min());//Infinity

            console.log(Math.min('aa));//NaN

 6.Math.round():  返回给定数值的四舍五入值

            console.log(Math.round(5.1));//5

            console.log(Math.round(5.5));//6

            console.log(Math.round(-1.5));//-1

            console.log(Math.round(-1.6));//-2

            console.log(Math.round());//NaN

  7.Math.pow():  返回给定数值的指数次幂

            console.log(Math.pow(4,2));//等于4*4=16

            console.log(Math.pow(2,-1));//等于1/2=0.5

            console.log(Math.pow(2,1.2));//等于2.2973967099940698

 8.Math.random(): 返回给定数值范围内的随机数

            console.log(Math.random());//0~1

            console.log(Math.random()*100);//0~100

            console.log(Math.random()*(99-1)+1);//1~99

9.Math.sqrt():   返回给定数值的平方根

            console.log(Math.sqrt(4));//等于2

            console.log(Math.sqrt(9));//等于3

10.Math.sin(x):  返回给定数的正弦值

            console.log(Math.sin(90 * Math.PI / 180));//1,sin(90度)

11.Math.cos(x):  返回给定数的余弦值

            console.log(Math.cos(60 * Math.PI / 180));//0.5,cos(45度)

            //这两个函数中的X 都是指的“弧度”而非“角度”   

            //弧度的计算公式为:角度*(Math.PI/180)

            //30° 角度 的弧度 = 30 * (Math.PI/180)

12.Math.trunc():  返回给定数值的整数部分,不管是正数还是负数,参数不是数值则结果为NaN

            console.log(Math.trunc(1.12));//等于1           

            console.log(Math.trunc(-1.12));//等于-1

            console.log(Math.trunc('str'));//NaN

     

  • 4
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值