js 的 Math 对象

用于执行数学任务:对 number 类型数据进行操作。

记录几个常用的:

1、Math.abs(x)

返回值为 x 的绝对值

Math.abs(-1);    //1

2、Math.ceil(x)

返回值为对 x 向上舍入的值

Math.ceil(1.11);    //2

3、Math.floor(x)

返回值为对 x 向下舍入的值

Math.floor(1.9);    //1

4、Math.max(x, y)

返回 x 和 y 中值大的数

Math.max(1, 2);    //2

5、Math.min(x, y);

与 max 相反,返回的是二者中值小的数

Math.min(1, 2);    //1

6、Math.pow(x, y)

返回值为 x 的 y 次幂,简单来说就是 x 的 y 次方。

Math.pow(2, 2);    //4
Math.pow(2, 3);    //8
Math.pow(2, 4);    //16

7、Math.random()

返回一个介于 0~1 间的随机数

这个不好举例子,因为我输出的跟你输出的相同几率极小。。。

Math.random();    //小数点后15~18位不等

注意没有参数嗷~

8、Math.round(x)

返回值为四舍五入后的整数

Math.round(1.5);    //2
Math.round(1.49);    //1

9、Math.sqrt(x)

返回值为该数的开平方数

Math.sqrt(9);    //3
原文地址:https://www.cnblogs.com/guofan/p/6605616.html