w3resource

PostgreSQL TRUNC() function

TRUNC() function

The PostgreSQL trunc() function is used to truncate a number to a particular decimal places. If no decimal places are provided it truncate toward zero(0).

Uses of TRUNC() Function
  • Precision Control: To control the precision of numerical values by truncating to a specified number of decimal places.

  • Data Formatting: Useful in formatting numerical data for reports and presentations.

  • Financial Calculations: Employed in financial calculations where specific decimal precision is required without rounding.

  • Mathematical Operations: To perform operations where truncation of numbers is necessary for intermediate steps.

  • Data Analysis: Utilized in data analysis tasks to manage and normalize numerical values by truncating excess decimal places.

Syntax:

trunc()

PostgreSQL Version: 9.3

Pictorial presentation of PostgreSQL TRUNC() function

pictorial presentation of PostgreSQL TRUNC() function

Example 1: PostgreSQL TRUNC() function

Code:

SELECT TRUNC(67.456) AS "Truncate";

Sample Output:

 Truncate
----------
       67
(1 row)

Example 2: PostgreSQL TRUNC() function

Code:

SELECT TRUNC(67.456,1) AS "Truncate upto 1 decimal";

Sample Output:

 Truncate upto 1 decimal
-------------------------
                    67.4
(1 row)

Example 3: PostgreSQL TRUNC() function

Code:

SELECT TRUNC(67.456,2) AS "Truncate upto 2 decimal";

Sample Output:

 Truncate upto 2 decimal
-------------------------
                   67.45
(1 row)

Previous: TAN function
Next: PostgreSQL ARRAY functions ARRAY_APPEND function



Follow us on Facebook and Twitter for latest update.