码迷,mamicode.com
首页 > 编程语言 > 详细

JavaScript中的Math对象

时间:2014-07-26 01:08:26      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:style   http   java   color   os   width   re   c   

Math对象中提供的计算功能执行起来比你在代码中写的js要快得多,这是它的一个很好的优点。

属性:
属性说明
Math.E即常量e的值
Math.LN10ln10
Math.LN2ln2
Math.LOG2E2为底e的对数
Math.LOG10E10为底e的对数
Math.PIπ
Math.SQRT1_21/2的平方根
Math.SQRT2 2的平方根

方法:
Math.min():求最小值
Math.max():求最大值

  1. Math.max(1,2,3,4,5) // 5
  2. Math.min(1,2,2,3,4) // 1
  3. Math.min.apply(Math,[1,2,3,4,5,6,7]); // 1

舍入方法:
Math.ceil() : 向上舍入
Math.floor() : 向下舍入
Math.round() : 四舍五入

  1. Math.ceil(2.1) ;// 3
  2. Math.ceil(2.8) ;// 3
  3. Math.ceil(2.5) ;// 3
  4. Math.ceil(2.0) ;// 2
  5. Math.floor(2.1) ; //2
  6. Math.floor(2.8) ;//2
  7. Math.floor(2.5) ;//2
  8. Math.floor(2.0) ;//2
  9. Math.round(2.1) ;//2
  10. Math.round(2.5) ;// 3
  11. Math.round(2.4) ;// 2
  12. Math.round(2.8) ;// 3
  13. Math.round(2.0) ;// 2

随机数:
random()方法:返回一个小数N,其中 0≤N<1,即 N∈[0,1) 

求某个范围的可能取值:
值 = Math.floor(Math.random() * 可能值得总数 + 第一个可能的值)

  1. //求1-10之间的任意一个数:
  2. var value = Math.floor(Math.random() * 10 + 1);
  3. //求2 - 10之间的任意一个数:
  4. var value2 = Math.floor(Math.random() * 9 + 2);
其他方法:
方法说明
Math.abs(num)num的绝对值
Math.exp(num)Math.E的num次幂
Math.log(num)num的自然对数
Math.pow(num,power)num的power次方
Math.sqrt(num)num的平方根
Math.cos(x)x的余弦值
Math.sin(x)x的正弦值
Math.tan(x)x的正切值
Math.acos(x)x的反余弦值
Math.asin(x)x的反正弦值
Math.atan(x)x的反正切值
Math.atan2(y,x)y/x的反正切值




JavaScript中的Math对象,布布扣,bubuko.com

JavaScript中的Math对象

标签:style   http   java   color   os   width   re   c   

原文地址:http://www.cnblogs.com/iceseal/p/3868680.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!