javaScript一些函數--Math()

來源:互聯網
上載者:User

標籤:

1.不能顯式地建立一個Math對象,直接使用它就可以了;

2.Math對象不能儲存資料,和String,Date對象不同;

3.前面知道了parseInt()函數會通過消去小數點後面的一切,來使一個小數變成整數(因此24.999變為24).經常我們需要更精確的計算。

於是通過Math對象的這幾個方法:

round():當小數是0.5或者大於0.5的時候,向上入一位;

ceil():始終向上舍入,因此23.75變成24,23.25也是如此;

floor():始終向下舍入,因此23.75變成23,23.25也是如此;

 1 <DOCTYPE html> 2 <html> 3     <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/> 4     <head> 5         <title>Math函數</title> 6     </head> 7     <script type="text/javascript"> 8        var userInput=prompt("請輸入一個數",""); 9        document.write("round()=",+Math.round(userInput));10        document.write("ceil()=",+Math.ceil(userInput));11        document.write("floor()=",+Math.floor(userInput));12        13     </script>14     <body>15     </body>16 </html>

4.可以使用Math對象的random()方法,產生一個大於等於0,但小於1的隨機小數。通常為了利用它,你需要再乘以某個數,然後在使用其中的一個舍入方法。

var diceThrow=Math.round(Math.random()*6)+1;
document.write("You threw a "+diceThrow);

 

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.