JavaScript Math 对象允许您对数字执行数学任务。
一、Math.PI;//返回3.141592653589793
**二、Math.round()**返回值是四舍五入最接近的整数。
例:
Math.round(6.8); // 返回 7
Math.round(2.3); // 返回 2
三、Math.pow() Math.pow(x, y) 的返回值是 x 的 y 次幂
例:
Math.pow(8, 2); // 返回 64
四、Math.sqrt() Math.sqrt(x) 返回 x 的平方根
例:
Math.sqrt(64); // 返回 8
五、Math.abs() Math.abs(x) 返回 x 的绝对(正)值
例:
Math.abs(-4.7); // 返回 4.7
六、Math.ceil() Math.ceil(x) 的返回值是 x 上舍入最接近的整数
例:
Math.ceil(6.4); // 返回 7
七、Math.floor() Math.floor(x) 的返回值是 x 下舍入最接近的整数
例:
Math.floor(2.7); // 返回 2
八、Math.sin() Math.sin(x) 返回角 x(以弧度计)的正弦(介于 -1 与 1 之间的值)
也可以将角度转换为弧度:Angle in radians = Angle in degrees x PI / 180.
例
Math.sin(90 * Math.PI / 180); // 返回 1(90 度的正弦)
九、Math.cos() Math.cos(x) 返回角 x(以弧度计)的余弦(介于 -1 与 1 之间的值)。
例:
Math.cos(0 * Math.PI / 180); // 返回 1(0 度的余弦)
十、Math.min() 和 Math.max() 可用于查找参数列表中的最低或最高值。
例:
Math.min(0, 450, 35, 10, -8, -300, -78); // 返回 -300
例:
Math.max(0, 450, 35, 10, -8, -300, -78); // 返回 450
W3School JavaScript
JavaScript学习:JavaScript数学 Math对象
最新推荐文章于 2023-07-21 08:35:26 发布