In this PostgreSQL post explains how to use the PostgreSQL trunc function with syntax and examples.
The PostgreSQL trunc function returns a number truncated to a certain number of decimal places.
The syntax for the trunc function in PostgreSQL is:
trunc( number, [ decimal_places ] )
The number to truncate.
Optional. The number of decimal places to truncate to. This value must be a positive or negative integer.
The trunc function can be used in the following versions of PostgreSQL:
Let's look at some PostgreSQL trunc function examples and explore how to use the trunc function in PostgreSQL.
For example:
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)