js中的Math对象

1.使用Math对象的min()和max方法获取数组最大值或最小值

2.使用Math对象的舍入方法:

Math.ceil():向上舍入

Math.floor():向下舍入

Math.round():标准四舍五入

3.生成随机数:

公式:值=Math.floor(Math.random()*可能值得总数 + 第一个可能的值)

<script type="text/javascript">
    //生成一个1至10的整数
        // var num=Math.floor(Math.random()*10+1);
        // console.log(num)
    //对上面进行封装一下,范围可自己选择 lowerValue ---- upperValue
        function randomNum(lowerValue,upperValue){
            return Math.floor(Math.random()*(upperValue-lowerValue+1)+lowerValue);
        }
        console.log(randomNum(1,100));

</script>

测试结果(刷新可看变化):

原文地址:https://www.cnblogs.com/xingguozhiming/p/8636036.html