Math对象和常用方法

在js中,有一些内置对象,可以直接使用

1. Math对象是js的内置对象,这个对象中存储了一些常用的数学常量和数学计算方法
console . log ( Math .PI);

2.E自然对数
console . log ( Math .E);

3. pow ;次方计算
console . log ( Math . pow ( 2 , 3 ));

4. sqrt: 开方计算
console . log ( Math . sqrt ( 9 ));
开方也可以用pow
console . log ( Math . pow ( 27 , 1 / 3 ));

5 abs:绝对值计算
console . log ( Math . abs (- 99 ));

6. max: 找出最大的数字
console . log ( Math . max ( 5 , 7 ));

7 .floor: 向下取整,取刚刚小于等于这个数字的整数
console . log ( Math . floor ( 3 , 6 ));

8. ceil :向上取整
console . log ( Math . ceil ( 3 , 6 ));

9 .round: 四舍五入
console . log ( Math . round ( 3 , 4 ));

10.sin,正弦函数 3.14弧度 = 180度
console . log ( Math . sin ( 30 / 180 * Math .PI)); //角度先转成弧度再计算

常用方法:
1. Math.random方法,生成一个[0,1)左闭右开的随机数
var num = Math . random ();
console . log ( num );

2. 生成一个0到100
var n1 = Math . random ()* 100 ;

n2 = Math . floor ( n1 );
console . log ( n1 );

3. 生成一个50到100
var n2 = Math . random ()* 50 + 50 ;

// 规律:Math.random() *(大数-小数)+偏移量
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值