Math 对象常使用的方法
Math 和其他对象不同,他不是一个构造函数
他属于一个工具类不需要创建对象,但是里面封装许多运算的属性和方法
1、math.abs() 求绝对值 干掉负号
求返回数的绝对值。
console.log(Math.abs(-3.00000001)); //3
console.log(Math.abs(7.25-10)) //2.75
console.log(Math.abs(-3,4)); //3 4被忽略了
2、Math.ceil()
可以对一个数进行向上取整,小数位只要有值就自动进1
console.log(Math.ceil(3.4)); //4
console.log(Math.ceil(-3.4)); //3
console.log(Math.ceil(3.499)); //4
console.log(Math.ceil(3.0000000001)); //4
console.log(Math.ceil(3.000000000)); //3
3、Math.floor()
可以对一个数值进行向下取整,小数部分会被舍掉
console.log(Math.floor(3.0000000001)); //3
console.log(Math.floor(3.49)); //3
console.log(Math.floor(3.59)); //3
console.log(Math.floor(-3.49)); //-4
console.log(Math.floor(-3.00000001)); //-4
console.log(Math.floor(-3.0000000)); //-3
4、Math.round()
四舍五入取整 向大(上、左)、小(下、右)
console.log(Math.round(3.5)); //4
console.log(Math.round(-3.5)); //-3
console.log(Math.round(-3.4)); //-3
console.log(Math.round(-3.6)); //-4
console.log(Math.round(3.499));//3
5、random()随机数
(1)可以生成一个0到1的 随机数
console.log(Math.random());
(2)生成1-10之间的随机数
console.log(Math.random()*10)
(3)生成1-6之间的随机数
console.log(Math.random()*5+1)
(4)生成x-y之间的随机数
console.log(Math.random()*(y-x)+x)
6、Math.max()最大值
Math.min()最小值
var max=Math.max(10,45,30,100);
var min=Math.min(10,45,30,100);
console.log(max);//100
console.log(min);//10
7、Math.pow(x,y); 返回 x 的 y 次幂
console.log(Math.pow(2,3)); //2*2*2 == 8
console.log(Math.pow(-2,4));
8、Math.sqrt(数据); 返回一个数的平方根。(开方运算)
console.log(Math.sqrt(0))
console.log(Math.sqrt(1))
console.log(Math.sqrt(9))
console.log(Math.sqrt(0.64))
console.log(Math.sqrt(-9))