单例内置对象--Math对象

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        /* 
        1.URL编码方法
        encodeURI()
        endcodeURIComponent()

        解码
        decodeURI()
        decodeURIComponent() 
        */

        /* 
        2.Math()
        min()
        max()
        */
       /* 
       3.舍入方法
       Math.ceil()  向上舍入为最接近的整数
       Math.floor()  向下舍入为最接近的整数
       Math.round()  执行四舍五入
       Math.fround()  返回数值最接近的单精度(32位)浮点值表示
       */

       console.log(Math.ceil(25.9));
       console.log(Math.ceil(25.5));
       console.log(Math.ceil(25.1));

       console.log(Math.floor(25.9));
       console.log(Math.floor(25.5));
       console.log(Math.floor(25.1));

       console.log(Math.round(25.9));
       console.log(Math.round(25.5));
       console.log(Math.round(25.1));

       console.log(Math.fround(0.4));
       console.log(Math.fround(0.5));
       console.log(Math.fround(25.9));

       /* 
       以下为打印值:
            26 单例内置对象.html:34:16
            26 单例内置对象.html:35:16
            26 单例内置对象.html:36:16
            25 单例内置对象.html:38:16
            25 单例内置对象.html:39:16
            25 单例内置对象.html:40:16
            26 单例内置对象.html:42:16
            26 单例内置对象.html:43:16
            25 单例内置对象.html:44:16
            0.4000000059604645 单例内置对象.html:46:16
            0.5 单例内置对象.html:47:16
            25.899999618530273
        */

        /* 
        4.random()
        随机数0-1
        包括0但是不包括1
        */
    //    随机数实例
    function selectFrom(lowerValue, upperValue) {
        let choices = upperValue - lowerValue + 1;
        return Math.floor(Math.random() * choices + lowerValue);
    }
    let num  = selectFrom(2, 10); // 抽取2到10之间的数,包括2和10
    console.log(num);
    // 例子二()随机抽取数组中的元素
    let colors = ['red', 'green', 'blue', 'yellow', 'black', 'purple', 'brown'];
    let color = colors[selectFrom(0, colors.length-1)];
    console.log(color);

    /* 
    5.Math对象的其他方法列表

    abs(x) 	返回数的绝对值。
    acos(x) 	返回数的反余弦值。
    asin(x) 	返回数的反正弦值。
    atan(x) 	以介于 -PI/2 与 PI/2 弧度之间的数值来返回 x 的反正切值。
    atan2(y,x) 	返回从 x 轴到点 (x,y) 的角度(介于 -PI/2 与 PI/2 弧度之间)。
    cos(x) 	返回数的余弦。
    exp(x) 	返回 e 的指数。
    sin(x) 	返回数的正弦。
    sqrt(x) 	返回数的平方根。
    tan(x) 	返回角的正切。
    toSource() 	返回该对象的源代码。
    valueOf() 	返回 Math 对象的原始值。
    */
    </script>
</body>
</html>

 

posted @ 2021-03-15 22:00  小白咚  阅读(78)  评论(0编辑  收藏  举报