min() 和 max() 方法

Math.max();获取一堆数中的最大值; Math.min();获取一堆数中的最小值;

      var testMin=Math.min(1,2,3,4,5,6,7),
          testMax=Math.max(1,2,3,4,5,6,7);
    
          console.log(testMin,testMax);//1,7

求下面数组中的最大值,最小值,用的是apply的方法;

var testAry=[1,2,3,4,5,2,43,5,6,7,8,9,34];
      var testMin=Math.min.apply(null,testAry),
          testMax=Math.max.apply(null,testAry);
    
          console.log(testMin,testMax);//1,43

 

舍入方法

  • Math.round() 四舍五入

  • Math.ceil() 向上取整

  • Math.floor() 向下取整

  • Math.abs() 取绝对值

 

  console.log(Math.ceil(25.9)); //26
  console.log(Math.ceil(25.5)); //26
  console.log(Math.ceil(25.1)); //26
  console.log(Math.round(25.9)); //26
  console.log(Math.round(25.5)); //26
  console.log(Math.round(25.1)); //25
  console.log(Math.floor(25.9)); //25
  console.log(Math.floor(25.5)); //25
  console.log(Math.floor(25.1)); //25

对于所有介于 25 和 26(不包括 26)之间的数值, Math.ceil() 始终返回 26,因为它执行的是向上舍入。 Math.round() 方法只在数值大于等于 25.5 时返回 26;否则返回 25。最后, Math.floor()对所有介于 25 和 26(不包括 26)之间的数值都返回 25。

总结: 0-1之间: round>5进1   ceil->都会进1  floor-> 不会变

random

获取0-1之间的随机小数

获取minNum到maxNum之间的随机整数 Math.round(Math.random()*(maxNum-minNum)+minNum)

 

其它方法

Math.abs(num) 返回 num 的绝对值 Math.asin(x) 返回 x 的反正弦值 Math.exp(num) 返回 Math.E 的 num 次幂 Math.atan(x) 返回 x 的反正切值 Math.log(num) 返回 num 的自然对数 Math.atan2(y,x) 返回 y/x 的反正切值 Math.pow(num,power) 返回 num 的 power 次幂 Math.cos(x) 返回 x 的余弦值 Math.sqrt(num) 返回 num 的平方根 Math.sin(x) 返回 x 的正弦值 Math.acos(x) 返回 x 的反余弦值 Math.tan(x) 返回 x 的正切值