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

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>
时间: 2024-09-19 09:02:57

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

Math的floor,round和ceil的总结

floor 返回不大于的最大整数 found 则是4舍5入的计算,入的时候是到大于它的整数 ceil 则是不小于他的最小整数 看例子   Math.floor Math.round Math.ceil 1.4 1 1 2 1.5 1 2 2 1.6 1 2 2 -1.4 -2 -1 -1 -1.5 -2 -1 -1 -1.6 -2 -2 -1 测试程序如下: public class MyTest { public static void main(String[] args) { double

PHP取整函数:ceil,floor,round,intval的区别详细解析_php技巧

我们经常用到的PHP取整函数,主要是:ceil,floor,round,intval. ceil -- 进一法取整说明float ceil ( float value ) 返回不小于 value 的下一个整数,value 如果有小数部分则进一位.ceil() 返回的类型仍然是 float,因为 float 值的范围通常比 integer 要大. PHP取整函数例子 1. ceil() 例子 复制代码 代码如下: < ?php echo ceil(4.3); // 5 echo ceil(9.99

Javascript Math ceil()、floor()、round()三个函数的区别_基础知识

下面来介绍将小数值舍入为整数的几个方法:Math.ceil().Math.floor()和Math.round(). 这三个方法分别遵循下列舍入规则: ◎Math.ceil()执行向上舍入,即它总是将数值向上舍入为最接近的整数: ◎Math.floor()执行向下舍入,即它总是将数值向下舍入为最接近的整数: ◎Math.round()执行标准舍入,即它总是将数值四舍五入为最接近的整数(这也是我们在数学课上学到的舍入规则). 下面是使用这些方法的示例: alert(Math.ceil(25.9))

JavaScipt中的Math.ceil() 、Math.floor() 、Math.round() 三个函数的理解_基础知识

首先还是看看JavaScript: The Definitive Guide, 4th Edition中对三个函数的定义. Math.ceil(): round a number up Arguments: Any numeric value or expression Returns: The closest integer greater than or equal to x. ---------------------------------------------------------

js中Math之random,round,ceil,floor的用法总结_javascript技巧

<SPAN style="FONT-SIZE: 18px">1.Math.random(); 结果为0-1间的一个随机数(包括0,不包括1) </SPAN>  Math.random(); 结果为0-1间的一个随机数(包括0,不包括1) [html] view plaincopyprint?<SPAN style="FONT-SIZE: 18px">  2.Math.floor(num); 参数num为一个数值,函数结果为num的

Javascript Math.ceil()与Math.round()与Math.floor()区别

Math.ceil()向上舍入 1 2 3 alert(Math.ceil(20.1)) //输出 21 alert(Math.ceil(20.5)) //输出 21 alert(Math.ceil(20.9)) //输出 21  Math.round标准的四舍五入 1 2 3 alert(Math.round(20.1)) //输出 20 alert(Math.round(20.5)) //输出 21 alert(Math.round(20.9)) //输出 21  Math.floor()向

JavaScript Math.ceil 方法(对数值向上取整)_基础知识

JavaScript Math.ceil 方法Math.ceil 方法用于对数值向上取整,即得到大于或等于该数值的最小整数.语法如下: Math.ceil(x) 参数说明: 参数 说明 x 必需.必须是一个数值. 提示:该方法与 Math.floor 方法正好相反. Math.ceil 方法实例 <script language="JavaScript"> document.write( Math.ceil(0.35) + "<br />"

php取整函数ceil,floo,round的用法及介绍_php技巧

ceil  是向上进位得到一个值的函数:floor 是舍掉小数位得到一个值的函数:round 是用来四舍五入的函数 ceil定义和用法:ceil() 函数向上舍入为最接近的整数. 复制代码 代码如下: ceil(x); 说明: 返回不小于 x 的下一个整数,x 如果有小数部分则进一位.ceil() 返回的类型仍然是 float. 例子: 复制代码 代码如下: <?php    echo ceil(0.60);    echo "<br/>";    echo ceil

JavaScript Math.floor方法(对数值向下取整)_基础知识

JavaScript Math.floor 方法Math.floor 方法用于对数值向下取整,即得到小于或等于该数值的最大整数.语法如下: Math.floor(x) 参数说明: 参数 说明 x 必需.必须是一个数值. 提示:该方法与 Math.ceil 方法正好相反. Math.floor 方法实例 <script language="JavaScript"> document.write( Math.floor(0.35) + "<br />&qu