今天项目让写随机数,然后遇到了Math对象里的各种函数。今天就把常用的记一下。以后遇到了在更新。
- 四舍五入(注意正负)
Math.round(4.5);//5
Math.round(-4.5);//-4
- 绝对值
Math.abs(-1);//1
- 取最大值
Math.max(2,1,3,4);//4
- 取最小值
Math.min(2,1,3,4);//1
- 往下取整(不会四舍五入,注意正负)
Math.floor(2.2);//2
Math.floor(2.6);//2
Math.floor(-2.6);//-3
- 往上取整(不会四舍五入,注意正负)
Math.ceil(2.2);//3
Math.ceil(-2.2);//-2
- 指数运算
Math.pow(2,3);//2的3次方
- 平方根(如果是负数就会返回NAN)
Math.sqrt(4);//2
- 随机数
Math.random();//返回一个0到1的随机数,注意不包括1
以后遇到了继续更新