Math对象一些简单方法

    /**
     * Math与其他对象不同,它不是一个构造函数
     * 它属于一个工具类不用创建对象,它里边封装了数学运算相关的属性和方法
     */
    console.log(Math.PI);// 圆周率:3.141592653589793
    console.log(Math.abs(-1));//返回绝对值1
    console.log(Math.ceil(1.37));//对于一个小数向上取整,2
    console.log(Math.floor(1.99));//向下取整,1
    console.log(Math.round(1.99));//四舍五入取整,2
    /**
     * random()生成一个0~1之间随机数
     * 生成一个0~10不包括两端的随机数
     * Math.round()*10;
     * 如果生成0~10之间的随机整数
     * Math.round(Math.random()*10)
     * 如果生成0~x之间的随机整数
     * Math.round(Math.random()*x)
     * 如果生成1~10之间的随机整数
     * Math.round(Math.random()*9+1)
     * 生成一个x~y的随机数
     * Math.round(Math.random()*9(y-x)+x)
     */
    console.log(Math.random());// 0.1220844289694597
    console.log(Math.random() * 10);// 9.93398452428683
    console.log(Math.round(Math.random() * 10));//6
    console.log(Math.round(Math.random() * 9 + 1));//乘以9是0~9之间随机整数,加一前后区间都加一,就是0~10
    /**
     * 获取多个数的最大最小值
     * max()和min()
     */
    console.log(Math.max(1, 5, 3, 2, 9, 0, 2));//9
    console.log(Math.min(1, 5, 3, 2, 9, 0, 2));//0
    /**
     * pow(x,y),返回x的y次幂
     */
    console.log(Math.pow(2, 0))//1
    /**
     * sqrt(x)返回x的平方根
     */
    console.log(Math.sqrt(5))// 2.23606797749979
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值