Introduction to PostgreSQL round
Whenever we deal with numeric values in the PostgreSQL database, the precision, and format in which we retrieve those values is of immense importance. The accuracy of the numbers carries a lot of importance in real-life use cases like for example precision of the measurements of certain aircraft or machine equipment or any other instrument, numeric values related to currency and transactions, etc. In this article, we will learn how we can round the numeric values into a particular integral value or up to the decimal points that we need while retrieving or manipulating the numeric data in the PostgreSQL database.
returned_value = ROUND (source_value [ , decimal_count ] )
Where the two parameters carry the following meaning –
- source_value: It is the numeric value or numeric expression that needs to be rounded to integer or the particular number of decimal points to maintain the precision.
- decimal_count: It is the optional parameter that specifies the number of decimal points up to which the source_value should be rounded. The default value of this integer parameter is 0 when we do not mention it.
- returned_value: It is the value that is returned by the round function which is most often of the datatype same as the data_type of source_value if we do not specify the second parameter decimal_count. If the second parameter value is specified then the datatype of the returned value is numeric.
Examples to Implement PostgreSQL round
Let us learn how we can use the round() function to round the numeric values in PostgreSQL with the help of examples:
Converting the numeric value to integers
Consider one number say 45.145 when this number is rounded to an integer using the ROUND() function, it rounds up to 45 because the decimal value after a point is not equal to or greater than 5 digit. Let us perform and see the results on the PostgreSQL terminal. For that our query statement will be as follows –
Now, let us check what happens if the digit after the decimal point is 5 or greater than that and observe the integer value retrieved. For that let’s take a number say 98.536. Rounding this number in PostgreSQL using the following query statement
Now let us manipulate the field of a certain table and try to round the value. For this let us create a table named educbademo with the numeric field as price and id integer using the following create a query.
CREATE TABLE educbademo (id INTEGER PRIMARY KEY, price DECIMAL);
And add few rows in it using the following query statements –
INSERT INTO educbademo VALUES(1,23.565);
INSERT INTO educbademo VALUES(2,98.148);
INSERT INTO educbademo VALUES(3,94.4616);
INSERT INTO educbademo VALUES(4,352.462);
INSERT INTO educbademo VALUES(5,87.1547);
let us confirm the contents of the table by using the following SELECT query –
SELECT * FROM educbademo;
Now, let us round the values of the column price to integral value using the round() function and the following query statement –
SELECT ROUND(price) FROM educbademo;
Rounding the Decimal Numbers
Now, instead of integer values, we will round the numbers to a particular decimal number with certain specified decimal points that we specify in the second parameter to the round() function. Let us see, how we can do this with the help of an example. Consider a decimal numeric number say 985.561. For rounded to two-digits the query statement should contain the integer parameter decimal_count in the round() function as 2 and statement should be as follows –
That will result in the following output because as the decimal digit after two points that is 1 is less than 5, so the number will be rounded as 985.56.
Now, if our number is 985.566 then while rounding to two digits the numeric value that will result is as follows using the below query statement –
That gives the following output with value 985.57 as the digit after two decimals 6 is greater than or equal to 5, hence the second digit value is increased by one and the output is 985.57 instead of 985.56.
Now, let us round the values of the certain column to decimal values using the round function. Consider the same table educbademo whose price column is to be rounded to two decimal points. For this, the query statement will be as follows –
SELECT ROUND(price,2) FROM educbademo;
The number is rounded to two digits and for the numbers having a value greater than or equal to 5, the decimal value at second place is increased by one and for all others, it is kept as it is. If we round the column values to 3 digits then the query statement will be as follows –
SELECT ROUND(price,3) FROM educbademo;
If we round the column values to 4 digits then the query statement will be as follows –
SELECT ROUND(price,4) FROM educbademo;
As we can see that 0 is appended at the end of the numeric value in case if the decimal value doesn’t contain any value in that decimal place.
Round() function is used in PostgreSQL database while dealing with numeric values. It helps in rounding the number to the integer value or up to any decimal place as mentioned in the function’s optional second parameter. When the second parameter is not specified it is considered as zero and the number is converted to an integer value.
This function can be used on numbers directly or the numeric values stored in the columns of the table in the database. The rounded value depends on the value of the digit just after the place after which the value is to be rounded. If it is greater than or equal to 5 then the value of the digit up to which the rounding is being made is increased by one.
This is a guide to PostgreSQL round. Here we discuss an introduction with syntax and example to implement with codes and outputs. You can also go through our other related articles to learn more –