js中的数学函数Math

Math称为数学函数,但是它属于对象类型

   typeof Math => "object"  

Math为数学函数,提供了很多操作数字的方法

Math常用方法

abs
取绝对值

  Math.abs(-10)=>10

ceil/floor
向上或者向下取整

  Math.ceil(12.444)=>13  //向上Math.ceil(-12.444)=>-12Math.floor(-12.444)=>-13  //向下Math.floor(12.444)=>12

round
四舍五入

  Math.round(-10.5)=>10

sqrt
开平方

  Math.sqrt(100)=>10

pow
取幂(n的m次方) Math.pow(n,m)

 Math.pow(2,10)=>1024

max/min
获取最大值和最小值

    Math.max(12,23,34,45,56)=>56   Math.min(12,23,34,45,56)=>12

PI
获取圆周率

 Math.PI=>3.141592653589793

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

for (var i=0;i<10;i++){    console.log(Math.random());}=>VM546:2 0.04970057257337013VM546:2 0.6231351064275747VM546:2 0.5078224023964912VM546:2 0.8352905252558647VM546:2 0.6559457705463638VM546:2 0.8613184309923703VM546:2 0.560912961148593VM546:2 0.007279315129435915VM546:2 0.8116470880389592VM546:2 0.7306956598107344undefined

思考:1-10之间的整数
Math.round(Math.random()*(m-n)+n):获取n-m之间的随机整数

   for (var i=0;i<10;i++){    console.log(Math.round(Math.random()*(69-24)+24));}=> VM675:2 58VM675:2 34VM675:2 64VM675:2 38VM675:2 34VM675:2 36VM675:2 33VM675:2 61VM675:2 33VM675:2 55undefined