javascript中Math函数的属性与方法

math函数的属性

Math.PI:返回圆周率。

math函数的方法

  1. 绝对值:

    Math.abs();

  2. 对数进行上舍入:

    Math.ceil();

  3. 对数进行下舍入:

    Math.floor();

  4. Math.pow(x, y); x的y次幂,y可以是分数

  5. 求最大最小值:Math.max();和Math.min();
    max和min方法中可以有多个值。

  6. 随机数:

     Math.random();  随生成一个0到1之间的随机数,包含0,不包含1 
        // 打印20到60之间的随机数
        var random = parseInt(Math.random() * (40+1) + 20);
        console.log(random);
        for (var i = 0; i < 100; i++) {
            random = parseInt(Math.random() * (40+1) + 20);
            console.log(random);
        }
        //写一个函数,用来打印m到n之间的一个随机数[m, n]
        function getRandom(m, n) {
            return parseInt(Math.random()*(n - m + 1) + n);
        }

猜你喜欢

转载自www.cnblogs.com/Yancyzheng/p/10461023.html