PostgreSQL:trunc函數
這個PostgreSQL教程解釋了如何在文法和示例中使用PostgreSQL trunc函數。
描述
PostgreSQL的TRUNC函數傳回一個數截斷到一定的小數位數。
句法
PostgreSQL中trunc函數的文法是:
trunc( number, [ decimal_places ] )
參數或參數
- 數
- 要截斷的數字。 decimal_places
- 可選的。要截斷的小數位數。該值必須是正整數或負整數。
注意
- 如果decimal_places是負數,則trunc函數将使小數位0值左側的數字成為數字。
- 又見輪,小區,天花闆和地闆的功能。
适用于
trunc函數可用于以下PostgreSQL版本:
- PostgreSQL 10.1 PostgreSQL 9.6 PostgreSQL 9.4,PostgreSQL 9.3,PostgreSQL 9.2,PostgreSQL 9.1,PostgreSQL 9.0,PostgreSQL 8.4
例
我們來看看一些PostgreSQL trunc函數示例,并探讨如何在PostgreSQL中使用trunc函數。
例如:
postgres=# SELECT trunc(125.315);
trunc
-------
125
(1 row)
postgres=# SELECT trunc(125.315, 0);
trunc
-------
125
(1 row)
postgres=# SELECT trunc(125.315, 1);
trunc
-------
125.3
(1 row)
postgres=# SELECT trunc(125.315, 2);
trunc
--------
125.31
(1 row)
postgres=# SELECT trunc(125.315, -1);
trunc
-------
120
(1 row)
postgres=# SELECT trunc(125.315, -2);
trunc
-------
100
(1 row)
postgres=# SELECT trunc(-125.315);
trunc
-------
-125
(1 row)