Math对象

63 阅读1分钟

一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第21天,点击查看活动详情

一。Math.ceil()值变大

<script>
          document.write(Math.ceil(25.4) + '<br>')
    // 26
    document.write(Math.ceil(-25.4) + '<br>')
    // -25
    document.write(Math.ceil(25.6) + '<br>')
    // 26
    document.write(Math.ceil(-25.6) + '<br>')
    // -25
    </script>

1.png

二。Math.floor()值变小

   <script>
           document.write(Math.floor(25.4) + '<br>')
    // 25
    document.write(Math.floor(-25.4) + '<br>')
    // -26
    document.write(Math.floor(25.6) + '<br>')
    // 25
    document.write(Math.floor(-25.6) + '<br>')
    // -26
    
    </script>

2.png

三。Math.round()四舍五入,负数小数点后为5舍去

   <script>
           document.write(Math.round(25.5) + '<br>')
    // 26
    document.write(Math.round(-25.5) + '<br>')
    // -25
    document.write(Math.round(25.4) + '<br>')
    // 25
    document.write(Math.round(-25.4) + '<br>')
    // -25
</script>

3.png

四。random()0.0-1.0随机数字,包括0但不包括1

   <script>
    document.write(Math.random());
</script>

5.png

五。Math.pow(a,b)a,代表底数,b代表几次方,b个a相乘

   <script>
    document.write(Math.pow(2,3));
</script>

8.png

六。抽奖公式

Math.floor(Math.random() * (max - min+1)) + min; 含最大值,含最小值

    <script>

    let arr = ['一等奖', '二等奖', '三等奖', '四等奖', '五等奖']
    let num = Math.floor(Math.random() * (4 - 0 + 1)) + 0;
    4代表五等奖,0代表一等奖
    document.write('恭喜你获得' + arr[num]);

</script>

78.png