js的Math对象介绍及其方法应用

Math的方法应用:
<script>
    //Math不是构造函数,不用创建对象,里边封装了数学运算相关的属性和方法;
    console.log(Math.PI);
    //abs()计算一个数的绝对值
    console.log(Math.abs(-3));
    // Math.ceil()对一个数进行向上取整,小数位只要有值,就自动进一;
    // Math.floor()对一个数向下取整,小数部分会被裁剪掉;
    console.log(Math.floor("1.99PX"));//NaN
    // round四舍五入去整
    console.log(Math.round(1.5));
    //random生成0-1之间的随机数
    console.log(Math.random());//0.4182032990235629
    //生成0-10之间案的随机数
    console.log(Math.random() * 10);//8.471469786693152
    // 生成x-y之间的随机数
    // console.log(Math.round(Math.random()*(y-x)+x))

    //获取最大值.min类似
    var max=Math.max(12,35,6,4,67);
    console.log(max);//67
    //或取x的y次方,开方运算
    console.log(Math.pow(5, 2));//25

</script>

猜你喜欢

转载自blog.csdn.net/weixin_44426449/article/details/107774698