Math对象 又叫数学对象,解决一些算术问题
1、Math.random()
Math.random()返回一个0-1之间的随机数
例1:取一个1-32的整数:a=parseInt(Math.random()*32)+1(取得到0但是取不到32,所以要加1)
例2:取一个0-9的整数:a=parseInt(Math.random()*10)
2、Math.abs()
Math.abs()返回一个数的绝对值(Math.abs(-23.4)=23.4)
3、Math.ceil()
Math.ceil()向上取整,只要有小数就进一位取整(Math.ceil(13.1)和Math.ceil(13.8)都返回14)
4、Math.floor()
Math.floor()向下取整,和parseInt相似,直接取整,不保留小数(Math.ceil(13.1)和Math.ceil(13.8)都返回13)
5、Math.max()
Math.max()返回最大值,如果只有一个数返回本身
6、Math.min()
Math.min()返回最小值,如果只有一个数返回本身
7、Math.pow()
Math.pow()返回指定数的次幂
例:Math.pow(2,3)返回值8(2的3次方)
8、Math.round()
Math.round()返回四舍五入的值
例:Math.round(12.4)返回值12,Math.round(12.5)返回值13
9、Math.PI
Math.PI放回值是圆周率3.141592653589793









网友评论