文章出處

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);

 


文章列表




Avast logo

Avast 防毒軟體已檢查此封電子郵件的病毒。
www.avast.com


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()