Math
对象的方法
序号 | 方法 | 用处 | |
---|---|---|---|
1 | Math.abs(num) | 绝对值 | |
2 | Math.round(num) | 四舍五入 | |
3 | Math.floor(num) | 向下取整,==parseInt | |
4 | Math.ceil() | 向上取整 | |
5 | Math.pow(x, y) | 求x 的y 次方结果 | |
6 | Math.sqrt(x) | 开平方 | |
7 | Math.max(...argunments) | 求最大值 | |
8 | Math.min(...arguments) | 求最小值 | |
9 | Math.random() | 随机数,返回 0-1之间的随机数 |
- 补充:
-
Math.round(num * 1000) / 1000
该方法默认是保留整数部分,乘除后可根据0
的个数保留小数位 -
paeseInt(Math.random()* (a - b)+ b)
[a, b)
parseInt(Math.random()* (a - b + 1)+ b)
[a, b]