js 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>

 

posted @ 2017-06-08 21:23  年少的你如此美丽  阅读(616)  评论(0编辑  收藏  举报