理顺 JavaScript (10) - Math 类


Math 和其他类不同, 它没有建立方法(不能这样使用: new Math()), 它的所有方法都是静态的(都得挂名调用).
Math.abs;    //绝对值
Math.max;    //两个数中的大者
Math.min;    //两个数中的小者
Math.random; //随机数
Math.round;  //四舍五入
Math.ceil;   //上舍入
Math.floor;  //下舍入
Math.exp;    //e 的指数
Math.log;    //自然对数
Math.pow;    //x 的 y 次方
Math.sqrt;   //平方根
Math.sin;    //正弦
Math.cos;    //余弦
Math.tan;    //正切
Math.asin;   //反正弦
Math.acos;   //反余弦
Math.atan;   //反正切
Math.atan2;  //从 X 轴到一个点的角度

 
 
 
 
 

 

 

  

Math 类的还有八个常数
alert(Math.E);       //2.718281828459045  - 自然对数的底数
alert(Math.LN10);    //2.302585092994046  - 10 的自然对数
alert(Math.LN2);     //0.6931471805599453 - 2 的自然对数
alert(Math.LOG10E);  //0.4342944819032518 - 以 10 为底的 e 的对数
alert(Math.LOG2E);   //1.4426950408889633 - 以 2 为底的 e 的对数
alert(Math.PI);      //3.141592653589793  - π
alert(Math.SQRT1_2); //0.7071067811865476 - 2 的平方根除 1
alert(Math.SQRT2);   //1.4142135623730951 - 2 的平方根

 
 
 
 
 

 

 

  

部分测试
/* 获取 100 以内的随机数 */
var n1, n2;
n1 = Math.ceil(Math.random()*100);
n2 = Math.ceil(Math.random()*100);
alert(n1); //9
alert(n2); //80

/* pow */
alert(Math.pow(2, 3));     // 8
alert(Math.pow(1.5, 2.4)); // 2.6461778006805154

/* round、ceil、floor*/
var x = 1.45;
alert(Math.round(x));  // 1
alert(Math.ceil(x));   // 2
alert(Math.floor(x));  // 1
x = -1.45;
alert(Math.round(x));  // -1
alert(Math.ceil(x));   // -1
alert(Math.floor(x));  // -2

 
 
 
 
 

 

 

  

转载于:https://my.oschina.net/hermer/blog/321016

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值