JavaScript 語法 流程控制 Function (函數) Object (物件) 數值 布林值 字串 陣列 時間和日期 Math 物件
正規表示式 瀏覽器物件模型 BOM JS DOM HTML DOM CSS樣式 & Event

Math Object

JavaScript Math Object

JavaScript 的 Math 物件 提供許多實用的 數學常數 和 數學計算函數 讓你可以直接使用。

Math 物件內建的 屬性 (Properties)

Math.E      Math 物件的 Math.E 屬性表示自然對數(natural logarithm)的基底(base)或稱 歐拉常數(Euler's constant),約2.718。
Math.LN2    Math 物件的 Math.LN2 屬性表示 2 的自然對數 (natural logarithm)。     Math.LN2;   // 0.6931471805599453
Math.LN10   Math 物件的 Math.LN10 屬性表示 10 的自然對數 (natural logarithm)。   Math.LN10;  // 2.302585092994046
Math.LOG2E  Math.LOG2E 屬性表示以 2 為基底 (base) e 的對數 (logarithm)。        Math.LOG2E;  // 1.4426950408889634
Math.LOG10E Math.LOG10E 屬性表示以 10 為基底 (base) e 的對數 (logarithm)。      Math.LOG10E; // 0.4342944819032518
Math.PI       Math.PI 屬性表示圓周率 π 3.14。        Math.PI;                  // 3.141592653589793
Math.SQRT1_2  SQRT1_2 靜態屬性(static property)代表2的平方根的倒數(square root of 1/2)  Math.SQRT1_2; // 0.7071067811865476
Math.SQRT2    Math.SQRT2 屬性表示 2 的平方根 1.414。 console.log( Math.SQRT2); // 1.4142135623730951

Math 物件內建的 方法 (Methods)

Math.abs()  Math.abs() 方法用來取得數字的 絕對值 (absolute value)。     Math.abs(x)
 Math.abs('-1');     // 1
 Math.abs(-2);       // 2
 Math.abs(null);     // 0
 Math.abs('');       // 0
 Math.abs([]);       // 0
 Math.abs([2]);      // 2
 Math.abs([1,2]);    // NaN
 Math.abs({});       // NaN
 Math.abs('string'); // NaN
 Math.abs();         // NaN
Math.ceil() Math.ceil() 方法用來對數字做 無條件進位,會返回大於等於傳入參數的最小整數值。 Math.ceil(x)
 Math.ceil(.95);    // 1
 Math.ceil(4);      // 4
 Math.ceil(7.004);  // 8
 Math.ceil(-0.95);  // -0
 Math.ceil(-4);     // -4
 Math.ceil(-7.004); // -7
Math.floor() Math.floor() 方法用來對數字做 無條件捨去,會返回小於等於傳入參數的最大整數值。 Math.floor(x)
 Math.floor( 45.95); //  45
 Math.floor( 45.05); //  45
 Math.floor(  4   ); //   4
 Math.floor(-45.05); // -46
 Math.floor(-45.95); // -46
Math.round() Math.round() 方法用來做四捨五入,round() 會四捨五入到最近的整數。 Math.round(x)
 Math.round(20.49);  //  20
 Math.round(20.5);   //  21            若想要四捨五入到 小數位數,可以用這樣的技巧:
 Math.round(42);     //  42            小數點兩位時: Math.round(x * 100) / 100
 Math.round(-20.5);  // -20            Math.round(1.5268 * 100) / 100   // 1.53
 Math.round(-20.51); // -21
Math.random() Math.random() 方法返回一個 0(包含) ~ 1(不包含)的隨機數(浮點數)。 Math.random() 注意:不帶參數
 Math.random();   // 0.23977311756820874
 Math.random();   // 0.11472062526665838
 Math.random();   // 0.9484832479755874
      例如: function getRandomInt(xxx) {
              return Math.floor(Math.random() * xxx);
            }
            console.log(getRandomInt(3));   // 輸出結果會是 0, 1 or 2
            console.log(getRandomInt(1));   //  0
            console.log(Math.random());     // 輸出一個 0 至 小於 1 的浮點數
Math.max() Math.max() 方法用來取得傳入所有傳入參數的 最大值。 Math.max([value1[, value2[, ...]]])
 Math.max(10, 20);     //  20
 Math.max(-10, -20);   // -10
 Math.max(-10, 20);    //  20
Math.min() Math.min() 方法用來取得傳入所有傳入參數的 最小值。 Math.min([value1[, value2[, ...]]]) Math.sin() Math.sin() 方法用來取得三角函數的 正弦值。 Math.sin(x)
 Math.sin(0);              // 0
 Math.sin(1);              // 0.8414709848078965
 Math.sin(Math.PI / 2);    // 1
Math.cos() Math.cos() 方法用來取得三角函數的 餘弦值。 Math.cos(x) Math.tan() Math.tan() 方法用來取得三角函數的 正切值。 Math.tan(x) Math.asin() Math.asin() 方法用來取得三角函數的 反正弦值。 Math.asin(x) Math.acos() Math.acos() 方法用來取得三角函數的 反餘弦值。 Math.acos(x) Math.atan() Math.atan() 方法用來取得三角函數的 反正切值。 Math.atan(x) Math.atan2() Math.atan2() 方法用來取得三角函數的 反正切值。 Math.atan2(y, x) Math.exp() Math.exp() 方法用來取得 e 的 x 次方。 Math.exp(x) Math.log() Math.log() 方法用來取得傳入參數的自然對數 (natural logarithm)。 Math.log(x) Math.pow() Math.pow() 方法用來做指數運算 base exponent。 Math.pow(base, exponent)
 Math.pow(7, 2);     // 49
 Math.pow(7, 3);     // 343
 Math.pow(2, 10);    // 1024
Math.sqrt() Math.sqrt() 方法用來取得傳入參數的 平方根 (square root)。 Math.sqrt(x)
 Math.sqrt(9);    // 3
 Math.sqrt(2);    // 1.414213562373095
 Math.sqrt(1);    // 1
 Math.sqrt(0);    // 0
 Math.sqrt(-1);   // NaN
Math.trunc() Math.trunc() 方法用來去除小數點,並回傳整數值。 Math.trunc(x)
 Math.trunc(-10.56) // -10
 Math.trunc(-2,10) // -2
 Math.trunc(0) // 0
 Math.trunc(9,245) // 9
 Math.trunc(30.45) // 30