Math 是 JS中的数学对象,属于一个静态对象;换句话说:在使用Math对象,不需要创建实例。
Math.PI
Math.PI
属性返回小数位为十五位的圆周率值。
console.log(Math.PI) // 3.141592653589793
Math.max()
Math.max([value1[, value2...]])
返回零个或多个数中的最大值,多个数之间用逗号 ,
隔开
value1, value2, ...
一组数值需要判断的数值
console.log(Math.max(1, 5, 4, 51, 23, 45, 16)) // 51
Math.min()
Math.min([value1[,value2, ...]])
返回零个或更多个数值的最小值
console.log(Math.max(1, 5, 4, 51, 23, 45, 16)) // 1
Math.abs()
Math.abs(x)
该方法用于返回指定的一个数的绝对值
console.log(Math.abs(-69)) // 69
console.log(Math.abs(69)) // 69
Math.ceil()
Math.ceil(x)
对传入的一个数进行向上取整(小数位不为零时,整数加1,小数去掉)
console.log(Math.ceil(5.000)) // 5
console.log(Math.ceil(5.0001)) // 6
Math.floor()
Math.floor(x)
向下取整(直接去掉小数)
console.log(Math.floor(9.999999999)) // 9
Math.round()
Math.round(x)
对传入的一个数进行四舍五入(通过小数点后一位决定是否四舍五入)
console.log(Math.round(5.5001)) // 6
console.log(Math.round(5.4999)) // 5
Math.pow()
Math.pow(x,y)
求x的y次方
console.log(Math.pow(2, 3)) // 8
Math.sqrt()
Math.sqrt(x)
求 x 的平方根
console.log(Math.sqrt(121)) // 11
Math.random()
Math.random()
返回一个0到1之间的随机小数; 范围[0,1),也就是说,从0(包括0)往上,但是不包括1(取不到1);该方法没有参数
console.log(Math.random()) // 0.7788394216557573