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

JavaScript 語法 Math Object

JavaScript Math Object

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

Math 物件內建的 屬性 (Properties)

JavaScript 提供了 8 個數學常數,可以作為數學屬性來存取: (與其他物件不同,Math 物件沒有建構函式。Math 物件是靜態的。)
任何數學屬性的語法是: Math.property
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 屬性代表2的平方根的倒數(square root of 1/2)    Math.SQRT1_2;  // 0.7071067811865476
Math.SQRT2    Math.SQRT2 屬性表示 2 的平方根 1.414。                 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()與 Math.floor()一起使用,可用於傳回隨機整數。
 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.random() 與 Math.floor()一起使用 傳回隨機 整數 )
<script>
  function randomInt() {
    var inputNum=document.getElementById("input_data").value;
    if(isNaN(inputNum)) {
      document.getElementById("you_data").innerText = inputNum ;
      document.getElementById("result_data").innerText =
                                       " 你輸入的不是數字!"; }
    else {
      document.getElementById("you_data").innerText =
               " 0 ~ "+ (inputNum-1) + " 之間的隨機數 為 : ";
      document.getElementById("result_data").innerText =
               Math.floor(Math.random() * inputNum); }
  }
</script>
  請輸入一個數字,來產生之間的隨機數 <br>
  <input id="input_data" type="text" value="" size="3" style="color:#339;">
  <input type="button" onClick="randomInt()" value="產生隨機數"><br><br>
  結果 :<output id="you_data"> </output> <output id="result_data"></output>
瀏覽器 網頁呈現:


請輸入一個數字,來產生之間的隨機數


結果 :



輸入 10 傳回 0~9 之間的隨機整數
輸入100 傳回 0~99 之間的隨機整數
輸入101 傳回 0~100之間的隨機整數

輸入100若要產生 1~100之間的隨機整數
將式子改成
Math.floor( Math.random() * 100) + 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[, ...]]])

三角函數 的定義

直角三角形 相異兩邊 的比值,
θ 為其一銳角之角度
此銳角的 對邊長 為 a ,
鄰邊長 為 b , 斜邊長 為 c
θ       □
c
     a


b
三角函數 :
sinθ = a / c
cosθ = b / c
tanθ = a / b
由於圓的周長為 2πr,(弧長 / 圓的周長)x360° = 角度
1 弧度的意思 定義為 弧長=半徑 r , ( r / 2πr )x360° = 角度
360°/ 2π 為一弧度。亦即360°= 2π 弧度。180°= 1π 弧度。
因此,角度 1°= ( 2π / 360°) 弧度 = ( π / 180°) 弧度。
Math.sin()    取得三角函數的 正弦值(傳回 -1 至 1 之間的值)。 Math.sin(x) 注意: x 為 弧度角=(角度數x Math.PI /180)
 Math.sin(0);              // 0
 Math.sin(1);              // 0.8414709848078965
 Math.sin(Math.PI / 2);    // 1
 檔案代碼:使用 test() Math.abs() Math.sin() Math.round()
計算 角度: <input type="text" onChange="sine(this)" value="" maxlength="5">
  ° 的 正弦值 為 <output id="sineB"></output>
<script>
  function sine(xx) {
    var degree = xx.value;
    var yy = "";
    if( /^-?\d{1,3}(\.\d{0,2})?$/.test(degree) && (Math.abs(degree) <= 360)){
      yy = Math.sin(degree * Math.PI / 180);         // test()以正規表達式判斷
      document.getElementById("sineB").innerText = Math.round(yy*100)/100 ;
    }
    else{document.getElementById("sineB").innerText= "輸入錯誤or超出角度範圍 !" 
    }
  }
</script>
瀏覽器 網頁呈現:

計算 角度 : ° 的 sin正弦值 為
Math.cos()     取得三角函數的 餘弦值(傳回 -1 至 1 之間的值)。  Math.cos(x)
Math.tan()     取得三角函數的 正切值。            Math.tan(x)
Math.asin()    取得三角函數的 反正弦值。          Math.asin(x)
Math.acos()    取得三角函數的 反餘弦值。          Math.acos(x)
Math.atan()    取得三角函數的 反正切值。          Math.atan(x)
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