Math 是一个内置对象,它拥有一些数学常数属性和数学函数方法。Math 不是一个函数对象。
Math 用于 Number 类型。它不支持 BigInt(表示大于 2^53 - 1 的整数)。
Math()对象的方法全部都是静态方法
以下介绍几个Math对象常用属性和方法:
- 调用圆周率
document.write(Math.PI)//3.141592653589793
- abs()求一个数的绝对值
document.write(Math.abs(-90))//90
- max()求若干数的最大值
document.write(Math.max(23,45,67,1,23,56))//67
- min()求若干数中的最小值
document.write(Math.min(23,45,67,1,23,56))//1
- pow(a,b) 求a的b次方
document.write(Math.pow(2,5))//32
- random() 返回一个浮点数, 伪随机数范围在[0,1)
document.write(Math.random())//0.9728475135303982
//结果随机
- ceil() 返回大于或等于一个给定数字的最小整数。
document.write(Math.ceil(1.2))//2
//天花板
- floor() 返回小于或等于一个给定数字的最大整数。
document.write(Math.floor(1.9))//1
//地板
- round() 四舍五入
document.write(Math.round(1.5))//2
document.write(Math.round(1.4))//1
- trunc()取整
document.write(Math.trunc(9.5))//9
- cbrt(x) 返回一个数的立方根
document.write(Math.cbrt(8))//2
- sqrt(x) 返回一个数的平方根
document.write(Math.sqrt(4))//2