深入理解RGBA、Math ceil()、floor()、round()、toFixed()随机数,取整方法

本文详细介绍了JavaScript中三种基本的数学运算方法:向上取整(Math.ceil())、向下取整(Math.floor())及四舍五入(Math.round())。通过具体的示例代码展示了这些函数如何处理正数和负数的小数部分,帮助读者更好地理解它们的工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里写图片描述

Math.ceil()
功能:对一个数进行上取整。
语法:Math.ceil(x)
参数:
x:一个数值。
返回值:返回大于或等于x,并且与之最接近的整数。
注:如果x是正数,则把小数“入”;如果x是负数,则把小数“舍”。
例:
<script type="text/javascript">
document.write( Math.ceil(1.2)+", "+Math.ceil(1.8)+", "+Math.ceil(-1.2)+", "+Math.ceil(-1.8) );
</script>
输出结果为:
document.write( Math.ceil(1.2)+", "+Math.ceil(1.8)+", "+Math.ceil(-1.2)+", "+Math.ceil(-1.8) ); 2, 2, -1, -1



Math.floor()
功能:对一个数进行下取整。
语法:Math.floor(x)
参数:
x:一个数值。
返回值:返回小于或等于x,并且与之最接近的整数。
注:如果x是正数,则把小数“舍”;如果x是负数,则把小数“入”。
例:
<script type="text/javascript">
document.write( Math.floor(1.2)+", "+Math.floor(1.8)+", "+Math.floor(-1.2)+", "+Math.floor(-1.8) );
</script>
输出结果为:
document.write( Math.floor(1.2)+", "+Math.floor(1.8)+", "+Math.floor(-1.2)+", "+Math.floor(-1.8) ); 1, 1, -2, -2




Math.round()
功能:四舍五入取整。
语法:Math.round(x)
参数:
x:一个数值。
返回值:与x最接近的整数。
例:
<script type="text/javascript">
document.write( Math.round(1.2)+", "+Math.round(1.8)+", "+Math.round(-1.2)+", "+Math.round(-1.8) );
</script>
输出结果为:
document.write( Math.round(1.2)+", "+Math.round(1.8)+", "+Math.round(-1.2)+", "+Math.round(-1.8) ); 1, 2, -1, -2 
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script type="text/javascript" src="js/jquery-2.1.0.min.js"></script>
        <style type="text/css">
            .no1 {
                width: 200px;
                height: 200px;
                border: 1px solid red;
                margin-right: 10px;
                display: block;
                float: left;
                text-align: center;
            }

            .num {
                clear: both;
            }
        </style>
        <script type="text/javascript">
            $(function() {
                $('.num').text("RGBA(0,0,0,0)");
                $(".no1").click(function() {
                    alert(1);
                });
                $(".no1").click(function() {
                    alert(2);
                });
                $(".no1").click(function() {
                    alert(3);
                });
                var setint = setInterval(function() {
                    var r = Math.floor(Math.random() * 255);
                    var g = Math.floor(Math.random() * 255);
                    var b = Math.floor(Math.random() * 255);
                    var a = Math.random().toFixed(2);
                    $('.num').text("RGBA(" + r + "," + g + "," + b + "," + a + ")");
                    $(".no1").css("background", "rgba(" + r + "," + g + "," + b + "," + a + ")");
                }, 500);
            })
        </script>
    </head>

    <body>
        <span class="no1">aaa</span>
        <span class="no1">bbb</span>
        <div class="num"></div>
    </body>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值