1.获取绝对值
Math.abs()
Math.abs(-12) === 12
2.向上取整
Math.ceil()
Math.ceil(12.03) === 13
3.向下取整
Math.floor()
Math.floor(12.3) === 12
4.四舍五入,取整
Math.round()
正数时,包含5是向上取整。负数时,包含5是向下取整。
Math.round(3.5) === 4
5.取[0,1)的随机小数
Math.random()
案例1:获取[0,10]的随机整数
console.log(parseInt(Math.random()*10));//未包含10
console.log(parseInt(Math.random()*10+1));//包含10
案例2:获取[n,m]之间的随机整数
Math.round(Math.random()*(m-n)+n)
6.获取一组数据中最大值
Math.max()
Math.max(9,100,200,45,78) === 200
7.获取一组数据中最小值
Math.min()
Math.min(10,1,9,100,200,45,78) === 1
8.获取圆周率Π的值
Math.PI
9.获取一个值的多少次幂
Math.pow()
Math.pow(10,2) === 100
10.对数值开方
Math.sqrt()
Math.sqrt(100) ===10