关于 Math 数学对象

181 阅读1分钟

1.获取圆周率 Math.PI

console.log(Math.PI)//3.141592653589793...

2.四舍五入 Math.round()

console.log(Math.round(1.7));//2
console.log(Math.round(2.4));//2

3.向上取整 Math.ceil()

console.log(Math.ceil(3.3))//4
console.log(Math.ceil(9.7))//10

4.向下取整 Math.floor()

console.log(Math.floor(1.8))//1
console.log(Math.floor(5.5))//5

5.求最大值 Math.max()

console.log(Math.max(40,50,60))//60

6.求最小值 Math.min()

console.log(Math.min(40,50,60))//40

7.生成0-1的随机数 Math.random()

console.log(Math.random())//[0,1),不包括1
console.log(Math.random()*100)//0~100的随机数,不包括100
console.log(parseInt(Math.random()*100))//0~100的整数,不包括100

8.幂运算 Math.pow(x,y) 求 x 的 y 次方

console.log(Math.pow(2,4))//16
console.log(Math.pow(2,-3))//0.125

9.幂的逆运算 (对数运算) Math.logx(y)

console.log(Math.log2(8));//3
console.log(Math.log10(100))//2

10.开平方 Math.sqrt()

console.log(Math.sqrt(100))//10

11.绝对值 Math.abs()

console.log(Math.abs(-20))//20