ES6 - Math.trunc()



此函式透過移除任何非整數數字返回數字的整數部分。

語法

Math.trunc() 函式的語法如下,其中,X − 表示一個數字。

Math.trunc( x ) ;

示例

<script>
   console.log(Math.trunc(-3.5)) // -3
   console.log(Math.trunc(-3.6)) // -3
   console.log(Math.trunc(3.5)) // 3
   console.log(Math.trunc(3.6)) // 3
</script>

以上程式碼的輸出如下所示:

-3
-3
3
3
廣告
© . All rights reserved.