Math数学函数及常用方法

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

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

Math.abs(10); :取绝对值

ceil / floor :向上或者向下取整

Math.ceil(10.01); //11
Math.ceil(-10.01);//-10

Math.floor(10.999);//10
Math.floor(-10.999);//-11

round:四舍五入

Math.round(10.49); //10
Math.round(-10.51);//-11

sqrt:开平方

Math.sqrt(100);//10
Math.sqrt(16);//4

pow:取幂(N的M次方)

Math.pow(2,10);// 2的10次方  1024

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

PI:获取圆周率

Math.PI ;//3.1415925653589793

random:获取随机小数

Math.random(); // 0.04689183

获取1-10之间的随机整数

Math.round(Math.random()*(m-n)+n);
Math.round(Math.random()*(30-20)+20);//20-30之间的随机整数

猜你喜欢

转载自www.cnblogs.com/xinxinxiangrong7/p/11432535.html