Math对象

128 阅读1分钟

1.1 Math对象

​ Math 对象不是构造函数,它具有数学常数和函数的属性和方法。跟数学相关的运算(求绝对值,取整、最大值等)可以使用 Math 中的成员。

属性、方法名功能
Math.PI圆周率
Math.floor()向下取整
Math.ceil()向上取整
Math.round()四舍五入版 就近取整 注意 -3.5 结果是 -3
Math.abs()绝对值
Math.max()/Math.min()求最大和最小值
Math.random()获取范围在[0,1)内的随机值
注意:上面的方法使用时必须带括号

获取指定范围内的随机整数

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