javaScript一些函数--Math()
生活随笔
收集整理的這篇文章主要介紹了
javaScript一些函数--Math()
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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()的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: js写分页
- 下一篇: 网站如何接入支付宝(转)