JavaScript的內建對象之Math對象

來源:互聯網
上載者:User

標籤:

一、Math對象概述:

  Math(算數)對象的作用是:執行常見的算數任務。儲存數學公式和資訊.

  與我們在JavaScript 直接編寫計算功能相比,Math 對象提供的計算功能執行起來要快得多。

  注意:

    Math 對象並不像 Date 和 String 那樣是對象的類,因此沒有建構函式 Math(),像 Math.sin() 這樣的函數只是函數,不是某個對象的方法。

    您無需建立它,通過把 Math 作為對象使用就可以調用其所有屬性和方法。

 

 

 

二、Math 對象的屬性:

    Math 對象包含的屬性大都是數學計算中可能會用到的一些特殊值。

   alert(Math.E);                         alert(Math.LN10);                    alert(Math.LN2);                    alert(Math.LOG2E);    alert(Math.LOG10E);    alert(Math.PI);    alert(Math.SQRT1_2);    alert(Math.SQRT2);

 

 

 

 

三、Math 對象的方法:

  1.min()和 max()方法:

    Math.min()用於確定一組數值中的最小值。

    Math.max()用於確定一組數值中的最大值。

    //max()方法    document.write(Math.max(5,7) + "<br />");    document.write(Math.max(-3,5) + "<br />");    document.write(Math.max(-3,-5) + "<br />");    document.write(Math.max(7.25,7.30));        //min()方法    document.write(Math.min(5,7) + "<br />");    document.write(Math.min(-3,5) + "<br />");    document.write(Math.min(-3,-5) + "<br />");    document.write(Math.min(7.25,7.30));        alert(Math.min(2,4,3,6,3,8,0,1,3)); //最小值    alert(Math.max(4,7,8,3,1,9,6,0,3,2)); //最大值

 

 

  2.舍入方法
    Math.ceil()執行向上舍入,即它總是將數值向上舍入為最接近的整數;

    alert(Math.ceil(25.9)); //26    alert(Math.ceil(25.5)); //26    alert(Math.ceil(25.1)); //26

    Math.floor()執行向下舍入,即它總是將數值向下舍入為最接近的整數;

    alert(Math.floor(25.9)); //25    alert(Math.floor(25.5)); //25    alert(Math.floor(25.1)); //25

    Math.round()執行標準舍入,即它總是將數值四捨五入為最接近的整數;

    alert(Math.round(25.9)); //26    alert(Math.round(25.5)); //26    alert(Math.round(25.1)); //25

 

 

  3.random()方法:

    該方法返回介於 0 到 1 之間一個隨機小數,不包括 0 和 1。

alert(Math.random());

    如果想擷取大於這個範圍的隨機數的話,可以套用一下公式:值 = Math.floor(Math.random() * 總數 + 第一個值)

  //隨機產生 1-10 之間的任意數    //先擷取隨機小數    var box  = Math.random();        //將擷取到的隨機小數,乘以10等到0到10之間的小數,不包括0和10,最後加上1就可以等到1到10之間的小數    box = box*10+1;        //將後面的小數截取掉(即將數值向下舍入),變成整數    box = Math.floor(box);    alert(box);        //寫成一句話就是:alert(Math.floor(Math.random() * 10 + 1)); 
  for (var i = 0; i<10;i ++) {        document.write(Math.floor(Math.random() * 10 + 5)); //5-14 之間的任意數 10+5-1=14        document.write(‘<br />‘);    }    //如果想要5到10 10-5+1 = 6 就是*6+5

    為了更加方便的傳遞想要範圍,可以定義一個函數:

  function selectFrom(lower, upper) {        var sum = upper - lower + 1;                                 //總數-第一個數+1        return Math.floor(Math.random() * sum + lower);    }        for (var i=0 ;i<10;i++) {        document.write(selectFrom(5,100));                             //直接傳遞範圍即可        document.write(‘<br />‘);    }

 

 

  4.其他方法

  

JavaScript的內建對象之Math對象

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.