【JavaScript】Math 对象常见方法详解
Math属于一个工具类,它不需要我们创建对象,它里边封装了属性运算相关的常量和方法,我们可以直接使用它来进行数学运算相关的操作。
方法 | 描述 |
---|---|
Math.random() | 生成一个[0-1)之间的随机数;生成一个x-y之间的随机数 |
Math.round() | 给定数字的值四舍五入到最接近的整数。 |
Math.ceil() | 向上取整 |
Math.floor() | 向下取整 |
Math.abs() | 绝对值运算 |
Math.max() | 求多个数中最大值 |
Math.min() | 求多个数中的最小值 |
Math.pow(x,y) | 求x的y次幂 |
Math.sqrt() | 对一个数进行开方 |
Math.PI | 常量,圆周率 |
下面分别讲述以上的一些重要方法的使用
Math.random()
函数返回一个浮点数,伪随机数在范围从0 到小于1,也就是说,从 0(包括 0)往上,但是不包括 1(排除 1),然后您可以缩放到所需的范围。实现将初始种子选择到随机数生成算法;它不能被用户选择或重置。
语法:Math.random()
返回值:一个浮点型伪随机数字,在0
(包括 0)和1
(不包括)之间。
// 生成 0 ~ 9 的随机整数
console.log(parseInt(Math.random() * 10)); // 0 ~ 9.9999999
// 生成 1 ~ 10 的随机整数
console.log(parseInt(Math.random() * 10) + 1);
Math.round()
函数返回一个数字四舍五入后最接近的整数。
语法:Math.round(x)
参数:x (一个数字)
返回值:给定数字的值四舍五入到最接近的整数。
x = Math.round(20.49); //20
x = Math.round(20.5); //21
x = Math.round(-20.5); //-20
x = Math.round(-20.51); //-21
Math.ceil()
函数总是四舍五入并返回大于等于给定数字的最小整数。
语法:Math.ceil(x)
参数:x (一个数字)
作用:对一个浮点数进行向下取整
返回值:大于等于 x
的最小整数。它的值与 -Math.floor(-x)
相同。
Math.ceil(-Infinity); // -Infinity
Math.ceil(-7.004); // -7
Math.ceil(-4); // -4
Math.ceil(-0.95); // -0
Math.ceil(-0); // -0
Math.ceil(0); // 0
Math.ceil(0.95); // 1
Math.ceil(4); // 4
Math.ceil(7.004); // 8
Math.ceil(Infinity); // Infinity
Math.floor()
函数总是返回小于等于一个给定数字的最大整数。
语法:Math.floor(x)
参数:x (一个数字)
作用:对一个浮点数进行向下取整
返回值:小于等于 x
的最大整数。它的值与 -Math.ceil(-x)
相同。
Math.floor(-Infinity); // -Infinity
Math.floor(-45.95); // -46
Math.floor(-45.05); // -46
Math.floor(-0); // -0
Math.floor(0); // 0
Math.floor(4); // 4
Math.floor(45.05); // 45
Math.floor(45.95); // 45
Math.floor(Infinity); // Infinity
Math.abs(x)
函数返回一个数字的绝对值。
语法:Math.abs(x)
参数:x (一个数字)
作用:取一个数的绝对值
返回值:x
的绝对值。如果 x
是负数(包括 -0
),则返回 -x
。否则,返回 x
。
console.log(Math.abs(-10.5)); // 10.5var a = -10;
console.log(a); // -10
console.log(Math.abs(a)); // 10
强制转换参数
Math.abs()
将其参数强制转换为数字。无法强制转换的值将变成 NaN
,使 Math.abs()
也返回 NaN
。
Math.abs("-1"); // 1
Math.abs(-2); // 2
Math.abs(null); // 0
Math.abs(""); // 0
Math.abs([]); // 0
Math.abs([2]); // 2
Math.abs([1, 2]); // NaN
Math.abs({}); // NaN
Math.abs("string"); // NaN
Math.abs(); // NaN
Math.min()
函数返回作为输入参数的数字中最小的一个,如果没有参数,则返回Infinity
。
语法:Math.min(数字1,数字2,数字3…)
作用:寻找一组数中的最小值
返回值:给定数值中最小的数。如果任一参数不能转换为数值,则返回 NaN
。如果没有提供参数,返回 Infinity
。
console.log(Math.min(10, 30, 50, 70 ,34)); // 10var arr = [1, 23, 4, 5, 34]
console.log(Math.min(...arr)); // 1
console.log(Math.min()); // Infinity
Math.max()
函数返回作为输入参数的最大数字,如果没有参数,则返回 -Infinity
。
语法:Math.max(数字1,数字2,数字3…)
作用:寻找一组数中的最大值
返回值:给定数值中最大的数。如果任一参数不能转换为数值,则返回 NaN
。如果没有提供参数,返回 -Infinity
。
console.log(Math.max(48, 29, 40, 28, -29)); // 48var arr = [1, 23, 4, 5, 34]
console.log(Math.max(...arr)); // 34
console.log(Math.max()); // -Infinityconst arr = [1, 2, 3];
const max = arr.reduce((a, b) => Math.max(a, b), -Infinity);
console.log(max); // 3
Math.PI
表示一个圆的周长与直径的比例,约为 3.14159
语法:Math.PI
作用:就是一个常量(π),经常当做常量用来参与运算。
console.log(Math.PI); // 3.141592653589793
console.log(Math.PI * 100); // 314.1592653589793
案例:使用 Math.PI 计算给定半径的圆周长
function calcul (radius) {return 2 * Math.PI * radius;
}
calcul(1); // 6.283185307179586
了解更多Math对象方法可以观看官方文档:Math.random() - JavaScript | MDN (mozilla.org)
得到两个数之间的随机整数并且包含着两个整数
function getRandom(min,max){return Math.floor(Math.random()*(max - min +1)) + min;
}
console.log(getRandom(2,6));
封装一个函数, 随机生成一个rgb颜色
function randNum(min, max) {return Math.round(Math.random() * (max - min)) + min;
}
function randColor() {return `rgb(${randNum(0, 255)}, ${randNum(0, 255)}, ${randNum(0, 255)})`;
}
console.log(randColor()); // rgb(100, 200, 244)