1. 程式人生 > >Math 數學的方法

Math 數學的方法

絕對值 -s console max 整數 mat 隨機 math 四舍五入

Math

// console.log(Math);// 對象;

1. Math.max : 獲取一組數的最大值
//console.log(Math.max(12, 3, 45, 109, 4));//109

2.Math.min
//console.log(Math.min(12, 3, 0, 109, 4));//0

3. Math.floor : 向下取整
//console.log(Math.floor(4.999));//4
//console.log(Math.floor(-4.999));//-5

4.Math.ceil() : 向上取整
//console.log(Math.ceil(4.01));// 5
//console.log(Math.ceil(-4.01));// -4

5.Math.round() : 四舍五入;保留整數;
console.log(Math.round(4.499));//4

6.Math.random();產生一個[0,1)的隨機小數;
//console.log(Math.random());
Math
.random()產生一個m--n之間的隨機整數;
//Math.round(Math.random()*(n-m)+m);
//Math.round(Math.random()*(90-40)+40)

7.Math.pow() :取數字的冪次方Math.pow(n,m) n的m次方
//console.log(Math.pow(2, 6));64

8.Math.sqrt() :開平方
//console.log(Math.sqrt(64));//8

9.Math.abs() :對負數取絕對值;
//console.log(Math.abs(-12));//12

10.Infinity 無窮大 -Infinity無窮小

Math 數學的方法