1. 程式人生 > >js Math [ 隨機數、絕對值、四舍五入、進一取整、舍去取整、最大值、最小值、圓周率 ]

js Math [ 隨機數、絕對值、四舍五入、進一取整、舍去取整、最大值、最小值、圓周率 ]

pre 最大 abs 取整 dom tran 絕對值 rip math

<script>
    /*
        數學對象:Math
    */
    with (document) {
        write(‘<br>-3.5的絕對值:‘+Math.abs(-3.5));
        write(‘<br>3.5的四舍五入:‘+Math.round(3.01));
        write(‘<br>3.01的進一取整:‘+Math.ceil(3.01));
        write(‘<br>3.99的舍去取整:‘+Math.floor(3.99));
        write(‘<br>獲取最大值:‘+Math.max(10,20,45,12));
        write(
‘<br>獲取最大值:‘+Math.min(10,20,45,12)); write(‘<br>獲取圓周率‘+Math.PI); // Math.random():獲取的>=0 <1的隨機數 write(‘<br>獲取隨機數‘+Math.random()); /* // 0-10的隨機數 Math.round(Math.random()*10); 0-0.5 0 0.5-1.5 1 1.5-2.5 2 9.5-10 10
*/ // 0 - 10的隨機數 console.log(Math.ceil(Math.random()*100000)%11); // 10-50的隨機數 /* 0-40 +10 */ console.log(Math.ceil(Math.random()*100000)%41+10); // m-n的隨機數 function getRandom(m, n){ return Math.ceil(Math.random()*100000)%(n-m+1)+m; } console.log(getRandom(
2,3)); } </script>

js Math [ 隨機數、絕對值、四舍五入、進一取整、舍去取整、最大值、最小值、圓周率 ]