1. 程式人生 > 其它 >js向上取整,向下取整,四捨五入,取絕對值等函式

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>