js向上取整,向下取整,四舍五入,取绝对值等函数

  1.向上取整。正数:有小数,舍弃小数,整数就加1。负数:就舍弃小数部分,取整数部分

  Math.ceil();

  2.向下取整。正数:舍弃小数,只要整数部分。负数:有小数,舍弃小数,整数减1

  Math.floor();

  3.取绝对值。正数就是本身,负数取反

  Math.abs();

  4.四舍五入。正数:第一位小数大于5,则整数加1,反之取整数。负数:第一位小数大于5,则取整数,反之整数减1

  Math.round()

  

<html>

<head>
<title></title>
</head>
    <script>
        window.onload=function(){
            var a=-4.1;
            alert(Math.ceil(a))  //弹框值-4
            var a=4.1;
            alert(Math.ceil(a))  //弹框值5
            var b=-4.9;
            alert(Math.floor(b)) //弹框值-5
            var b=4.9;
            alert(Math.floor(b)) //弹框值4
            var c=-4.5;
            alert(Math.round(c)) //弹框值-4
            var c=4.5;
            alert(Math.round(c)) //弹框值5
            var d=-7;
            alert(Math.abs(d))   //弹框值7
        }
        
        
    </script>
<body>
</body>

</html>

猜你喜欢

转载自www.cnblogs.com/superCwen/p/9842307.html