Math 數(shù)學函數(shù),提供了一些操作數(shù)字的方法
1,Math.abs 取絕對值 將負數(shù)變?yōu)檎龜?shù)
- Math.abs(-5); //5
2,Math.ceil() 向上取整 往大的方向取整
- Math.ceil(1.02); //2
- Math.ceil(-1.7); //-1
3,Math.floor() 向下取整 往小的方向取整
- Math.floor(1.9); //1
- Math.floor(-1.2); //-2
4,Math.max(); 從一堆數(shù)中查找最大值
- Math.max(1,4,3,6,3,21,7); //21
5,Math.min(); 從一堆數(shù)中查找最小值
- Math.min(1,4,3,6,3,21,7); //1
6,Math.round(); 四舍五入
- Math.round(1.3333); //1
- Math.round(1.5); //3
- 正數(shù)小數(shù)部分等于5就可以往大進位取值?。。?/li>
- Math.round(-1.4); //-1
- Math.round(-1.5); //-1
- 負數(shù)小數(shù)部分大于5才可以取更小的值,否則往大取值?。?!
- Math.round(-1.6); //-2
7,Math.random(); 產生一個隨機數(shù)[0,1)
- Math.round(Math.random()*(m-n)+n); 求某n-m之間的隨機整數(shù),包括n和m
- Math.round(Math.random()*10);
- Math.round(Math.random()*(10-0)+0);
- Math.round(Math.random()*(100-70)+70);
- Math.round(Math.random()*(70-61)+61);
8,Math.PI 圓周率Math的一個屬性
- Math.PI //不能加分號,這是Math的屬性!
9,Math.pow(x,y); x的y次冪
- Math.pow(2,3); //8
10,開平方得到平方根 只能得到正數(shù) 只能是[0,+∞)才能開平方;
- Math.sqrt(16); // 4
- Math.sqrt(15); // 3.872983346207417