JS.Math常用方法大全

1.Math.ceil()对数进行向上取整 返回大于或等于给定数字的最小整数

    console.log(Math.ceil(0.95));//1
    console.log(Math.ceil(4));//4

2.Math.floor()向下取整

    console.log(Math.floor(45.95));//45
    console.log(Math.floor(-45.05));//-46

3.Math.round()返回一个数字四舍五入后最接近的整数

    console.log(Math.round(45.95));//46
    console.log(Math.round(20.5));//21
    console.log(Math.round(-20.5));//-20
    console.log(Math.round(-20.51));//-21

4.Math.max/min(value1,value2,value3...) 返回给定的一组数字中的最大/最小值

    console.log(Math.max(1,2,3,78)); //78

5.Math.abs()取绝对值

console.log(Math.abs(-3));//3

6.Math.pow()获得幂的值

console.log(Math.pow(2, 3));//8

7.Math.random()随机数

        Math.rnd = (max, min = 0) => {
            return Math.round(Math.random() * (max - min)) + min
        }
        console.log(Math.rnd(30, 20));//23

猜你喜欢

转载自blog.51cto.com/14648170/2497355