js入門篇之Math對象

來源:互聯網
上載者:User

標籤:

Math對象用於執行數學任務

Math對象的屬性:

Math對象的方法:

常用屬性和方法:

Math.PI ----------------返回圓周率3.14 ...

Math.ceil(x) ------------對數值x進行向上取整

Math.floor(x) -----------對數值x進行向下取整

Math.round(x) ----------對數值x進行四捨五入

Math.min(a,b,c...) -------返回abc...中的最小值

Math.max(a,b,c...) -------返回abc...中的最大值

Math.random() --------返回介於0 ~ 1 之間的隨機數

設一個數字變數,當這一變數大於500的時候, 讓這個變數的值等於500;

var num;

實現1:if ( num > 500 ) { num = 500;}

 

實現2:num = Math.min( num ,500 );

Math.random ( ) 返回介於0~1之間的隨機小數;大於等於0 小於 1;

作為一名程式猿,相信大家對於文本資訊非常厭煩,本人也是,所以我之前的隨筆很少發文字內容,大部分都是代碼,所以這次也不例外。NO BB GIVE ME YOUR CODE。。。

<!doctype html><html><head><meta charset="utf-8"><title>標題</title><meta name="keywords" content=""><meta name="description" content=""><style>    *{margin:0; padding:0; list-style:none;}    </style></head><body>    <h1 id="con"></h1><script>    var con=document.getElementById(‘con‘);    var x1=18.0001;    var x2=18.9999;    // con.innerHTML=Math.PI;//3.1416926...    // con.innerHTML=Math.ceil(x1);//19    // con.innerHTML=Math.ceil(x2);//19    // con.innerHTML=Math.floor(x1);//18    // con.innerHTML=Math.floor(x2);//18    // con.innerHTML=parseInt(x1);//18    // con.innerHTML=parseInt(x2);//18    // con.innerHTML=Math.round(x1);//18    // con.innerHTML=Math.round(x2);//19    // con.innerHTML=Math.min(12,8,54,105,27,3,71);//3    // con.innerHTML=Math.max(12,8,54,105,27,3,71);//105    var timer=setInterval(function (){//大家試試,會彈出來嗎?        var abc=Math.random();        if (abc==0||abc==1) {            clearInterval(timer);            alert(‘警告‘);        };        con.innerHTML=abc;    },10);    /*if (0.000000==0) {        alert(‘hjjkb‘);    };*/</script></body></html>

 

js入門篇之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.