JS中的数学函数Math(提供的常用方法)
Math称为数学函数,但是它属于对象类型的
typefo Math //=> "object"
abs
aba:取绝对值
Math.abs(10) // => 10
Maht.abs(-10) //=> 10
ceil / floor
ceil/floor:向上或向下取整
Math.ceil(10) //=> 10
Math.ceil(10.01) // =>11
Math.ceil(-10.01) //=>-10
Math.floor(10.999) //=> 10
Math.floor(-10.099) //=> -11
round
round:四舍五入
Math.round(10.49) // =>10
Math.round(10.5) //=>11
Math.round(-10.49) // => -10
Math.round(-10.5) // =>-10
Math.round(-10.51) // => -11
random
random:获取0~1之间的随机小数
Math.round(Math.random()*(m-n)+n)
Math.round(Math.random()*(m-n)+n):获取n~m之间随机整数(到临界点的概率比较低)
sqrt
sqrt:开平方
Math.sqrt(100) //=>10
Math.sqrt(10) // =>3.1627766
Math.sqrt(16) // =>4
pow
pow:取幂(N的M次方)
Math.pow(2,10) // => 1024
max/min
max/min:获取最大值和最小值
Math.max(12,23,34,15,26) // => 34
Math.min(12,23,34,15,26) // => 12
PI
PI:获取圆周率
Math.PI // = 3.141592654