JavaScript中Math的函数

845 阅读1分钟

Math.sqrt是返回数的平方根

Math.sqrt(9) 等于3;

Math.abs是返回x的绝对值

Math.abs(-2)等于2;

Math.pow是返回x的y次幂

Math.pow(4,3)等于64;

Math.floor是向下取整数

Math.floor(3.8)等于3;

Math.ceil是向上取整数

Math.ceil(3.8)等于4;

Math.max是取一组数中的最大一项

Math.max(23, 45, 6, 2, 4, 5, 234, 6, 45)等于234;

Math.min是取一组数中的最小一项

Math.max(23, 45, 6, 2, 4, 5, 234, 6, 45)等于2;

Math.round是四舍五入为最接近的整数

Math.round(3.4)等于3;
Math.round(3.6)等于4;

Math.random是[0-1)之间的随机小数(注:包含0但不包含1)

Math.random() * 9是[0-9)之间的随机数;
Math.random * 10 + 1是[1-11)之间的随机数;

[n-m)之间的随机数

Math.random * (m - n) + n;

[n-m]之间的随机整数

Math.round(Math.random * (m - n) + n);