Round off column in PostgreSQL (Round() Function)

The ROUND() function in PostgreSQL is used to round a numeric value to the nearest integer or to a specified number of decimal places. This function is useful for formatting numbers for display or performing calculations where specific precision is required. In order to Round off the column in PostgreSQL table we use ROUND() function.

  • Round off the value in PostgreSQL – ROUND()
  • Round off value to decimal places in PostgreSQL
  • Create the column which round off the value of the column

With an example for each

Round off column in PostgreSQL (Round() Function) 1

 

Syntax:

ROUND(numeric_expression [, decimal_places])

  • numeric_expression: The number you want to round.
  • decimal_places (optional): The number of decimal places to round to. If omitted, the number is rounded to the nearest integer.

 

Get ROUND() in PostgreSQL:

Example 1: Rounding to the Nearest Integer

ROUND() function in PostgreSQL gets the round off value to nearest integer

 

SELECT ROUND(5.733) AS round_off;

So the round off value will be

Round off column in PostgreSQL (Round() Function) 2

 

 

Example 2: Rounding to a Specified Number of Decimal Places

To round the number 5.69677 to two decimal places:

 

SELECT ROUND(5.69677,2) AS round_off;

So the round off value will be for two decimal places

Round off column in PostgreSQL (Round() Function) 3

 

Example 3: Rounding Negative Numbers

To round the number -23.456 to the nearest integer:

 

SELECT ROUND(-23.456) AS rounded_number;

So the round off value will be

Round off column in PostgreSQL (Round() Function) 4

 

Example 4: Rounding Negative Numbers to 2 decimal place

To round the number -23.456 to 2 decimal places:

 

SELECT ROUND(-23.456,2) AS rounded_number;

So the round off value will be for two decimal places

Round off column in PostgreSQL (Round() Function) 5

 

Get ROUND() of column in PostgreSQL table:

We use table states

Round off column in PostgreSQL (Round() Function) 6

 

SELECT *,ROUND(hindex_score) as Round_off  FROM states

We have created a column and stored round off value of hindex_score to nearest integer

So the resultant table will be

Round off column in PostgreSQL (Round() Function) 7

 

Round off the column to decimal places in PostgreSQL table:

Rounding off column to two decimal places is shown below

 

SELECT *,ROUND(hindex_score,2) as Round_off  FROM states

We have created a column and stored round off value of hindex_score to two decimal places

So the resultant table will be

Round off column in PostgreSQL (Round() Function) 8

 

 

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts