JavaScripts基础(9)数学对象Math

233 阅读1分钟

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

typeof Math -> 'object'

之所以叫做数学函数,因为Math这个对象中提供了很多操作数字的方法

Math中提供的常用方法Math.XXX(n)

abs: 取绝对值,

ceil/floor: 向上/向下取整

Math.ceil(10.01)  =>11
Math.ceil(-10.01)  =>-10

Math.floor(10.99)  =>10
Math.floor(-10.99)  =>-11

round: 四舍五入(正数.5向上,负数.5向下)

sqrt: 开平方

pow: 取幂 (n的m次方)

Math.pow(2,10)  =>1024

max/min: 取最大值、取最小值

Math.max(1,2,3,4)   //4
Math.min(1,2,3,4)   //1

PI:获取圆周率

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

Math.round(Math.random()*(m-n)+n):获取n->m之间的随机正整数