单例内置对象--Math对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<!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 @   小白咚  阅读(81)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示