PostgreSQL Round down – Floor() function

FLOOR() Function in PostgreSQL is used to round down the value.

  • Get Round down value in PostgreSQL – FLOOR()
  • Create the column which extracts the round down value i.e. Floor of the column in the PostgreSQL table

 

PostgreSQL Round down – Floor() function 01

Syntax for FLOOR() Function in PostgreSQL:

SELECT floor(number_or_expression);

number_or_expression: This is the number or expression for which you want to round down or find the floor value to it.

 

FLOOR() Function in PostgreSQL:

FLOOR() function in PostgreSQL returns the round down value, i.e. the largest integer value that is less than or equal to a specified number.

Example 1:

 

SELECT FLOOR(4.56);

Output:

PostgreSQL Round down – Floor() function 2

 

Example 2:

 

SELECT FLOOR(-3.14);

Output:

PostgreSQL Round down – Floor() function 2b

 

 

 

Floor() function (Round down value) of the column in PostgreSQL :

you can use floor() function within a SELECT statement to calculate the largest integer less than or equal to each value in the column. Here’s how you can do it with the help of an example

bookkit:

PostgreSQL Round down – Floor() function 3

 

Example 1:

Creating the column which extracts the round down value i.e. Floor of the column in the PostgreSQL table is shown

 

select *,floor(charges) as floor_charges from bookkit
  • ”floor(charges)” calculates the largest integer less than or equal to each charges.
  • ”AS floor_charges” assigns an alias ”floor_charges”  to the calculated floor value.

So the output will be

Output:

PostgreSQL Round down – Floor() function 4

 

 

Round down only for the positive valued column in Postgresql

Example 1 – using Floor() function:

We will be rounding down only the positive column in the example let’s take “price” column.

 

select *,floor(price) as floor_price from bookkit

Output:

PostgreSQL Round down – Floor() function 5

 

Example 2 – using TRUNC() function:

We will be using TRUNC() function rounding down only the positive column in the example let’s take “price” column.

 

select *,TRUNC(price) as floor_price from bookkit

The truncate function will get the largest integer less than or equal to each value in the column,

Note: This function is applicable only when the entire column has positive Values

Output:

PostgreSQL Round down – Floor() function 6

 

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.