文章目录
Math 的常用方法
Math.abs 求绝对值
…
Math.abs(-1) //==>1
…
Math.cell 向上取整(进一法)
无论是正数还是负数,永远取最大的值
…
Math.cell(1.2) //>2
Math.cell(-1.2) //>-1
…
Math.floor 向下取整(直接取整)
无论是正数还是负数,都取最小的值
…
Math.floor(1.8) //>1
Math.floor(-1.8) //>-2
…
Math.round 四舍五入
正数的话,还是正常的,之前理解的,但是如果是负数,'临界点'必须大于5
…
Math.round(2.4) //>2
Math.round(2.5) //>3
Math.round(-2.5) //>-2
Math.round(-2.51) //>-3
Math.round(-2.4) //==>-2
…
Math.random() 获取0~1之间的随机数(大于等于0,不等于1)
获取n 到m 之间的随机数:Math.random()*(m-n)+n
// 获取0~100之间的随机数
…
Math.random()*100+0
…
Math.random是取[0,1)的随机数;
取[min,max]的随机整数时使用如下公式:
Math.floor(Math.random().(max-min+1)+min)
取[min.max)的随机整数时使用如下公式:
Math.floor(Math.random().(max-min)+min)
取(min,max]的随机整数时使用如下公式:
Math.floor(Math.random().(max-min)+min+1)
Math.sqrt() 开平方
…
Math.sqrt(4) //==>2
…
Math.pow(n,m) 取幂
…
Math.pow(2,2) //>4
Math.pow(2,3) //>8
…
Math.PI π
…
Math.PI //==>3.141592653589793
…
Math.max/Math.min 最大值和最小值
…
Math.max(1,5,9) //>9
Math.min(1,5,9) //>1
…