JavaScript 数学

JavaScript Math 数学

  • 神奇的圆周率
Math.PI ;  // 返回 3.1415926535……

Math 数学方法

Math.round()

  • Math.round(X):返回 X 的四舍五入的接近值整数
Math.round(6.8) ; // 返回 7
Math.round(3.14) ;  // 返回 3

Math.pow()

  • Math.pow(X,Y):返回 X 的 Y 次幂
Math.pow(3,4) ;  // 返回 81

Math.sqrt()

  • Math.sqrt(X):返回 X 的平方根
Math.sqrt(64) ;  // 返回 8 

Math.abs()

  • Math.abs(X):返回 X 的绝对值
Math.abs(-10) ; // 返回 10

Math.ceil()

  • Math.ceil(X):X向下(小)四舍五入的最接近的整数

Math.floor()

  • Math.floor(X):X向上(大)四舍五入的最接近的整数

Math.sin()

  • Math.sin(X):返回角X的正弦值

Math.cos()

  • Math.cos(X):返回角X的余弦值

Math.max()/min()

  • 返回列表中最大/最小 值

Math.random()

  • Math.random():返回介于 0 <= x < 1
Math.random();  //返回随机数

Math属性(常量)

Math.E		// 返回欧拉指数
Math.PI 	// 返回圆周率PI
Math.SQRT2	// 返回 2 的平方根
Math.SQRT1_2// 返回1/2的平方根
Math.LN2	// 返回 2 的自然对数
Math.LN10	// 返回 10的自然对数
Math.LOG2E	// 返回以 2 为底的 e 的对数
Math.LOG10E // 返回以 10为底的 e 的对数

JavaScript 随机数

Math.random()

  • Math.random() 总是返回小于 1 的数

随机整数

  • 因为random()返回的数是小于的数且有小数存在,所以可以利用floor()返回一个随机整数
Math.floor(Math.random() * 10) ; 		// 返回 0 ~ 9 之间的整数
Math.floor(Math.random() * 11) ; 		// 返回 0 ~ 11 之间的整数
Math.floor(Math.random() * 101); 		// 返回 0 ~ 100 之间的整数
Math.floor(Math.random() * 10) + 1 ;	// 返回 1 ~ 10 之间的整数
Math.floor(Math.random() * 100) + 1 ;	// 返回 1 ~ 100 之间的整数

一个随机“函数”

function getRndInteger(min,max) {
    return Math.floor(Math.random() * (max - min + 1)) + min ;
}

JavaScript 逻辑

布尔值

  • JavaScript接收 truefalse

Boolean()函数

Boolean(10 > 9) ;   // 返回 true

比较运算符

运算符 描述
== 等于
=== 真等于
!= 不等于
!== 真不等于
> 大于
< 小于
‘>= 大于等于
’<= 小于等于

逻辑运算符

运算符 描述
&&
||
三目运算符
posted @ 2019-06-20 18:43  Mirror王宇阳  阅读(250)  评论(0编辑  收藏  举报