首页 > Web开发 > 详细

JS.Math常用方法大全

时间:2020-05-21 16:07:15      阅读:55      评论:0      收藏:0      [点我收藏+]
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

JS.Math常用方法大全

原文:https://blog.51cto.com/14648170/2497355

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!