Updated May 12, 2023
Introduction to PostgreSQL round
Whenever we deal with numeric values in the PostgreSQL database, the precision and format in which we retrieve those values are of immense importance. The accuracy of the numbers carries a lot of importance in real-life use cases like, for example, the precision of the measurements of certain aircraft or machine equipment or any other instrument, numeric values related to currency and transactions, etc. This article will learn how to round the numeric values into a particular integral value or up to the decimal points we need while retrieving or manipulating the numeric data in the PostgreSQL database.
Syntax:
returned_value = ROUND (source_value [ , decimal_count ] )
Where the two parameters carry the following meaning –
- source_value: You need to round the numeric value or numeric expression to an integer or a specific number of decimal points to maintain precision.
- decimal_count: The optional parameter specifies the number of decimal points the source_value should be rounded up. The default value of this integer parameter is 0 when we do not mention it.
- returned_value: If you do not specify the second parameter decimal_count, the round function returns a value that is most often of the same data type as the data_type of source_value.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 –
Code:
SELECT ROUND(45.145);
Output:
Let’s observe the integer value retrieved when the digit after the decimal point is 5 or greater than 5. For that, let’s take a number, say 98.536. Rounding this number in PostgreSQL using the following query statement
Code:
SELECT ROUND(98.536);
Output:
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.
Code:
CREATE TABLE educbademo (id INTEGER PRIMARY KEY, price DECIMAL);
Output:
And add a few rows in it using the following query statements –
Code:
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);
Output:
let us confirm the contents of the table by using the following SELECT query –
Code:
SELECT * FROM educbademo;
Output:
Now, let us round the values of the column price to integral value using the round() function and the following query statement –
Code:
SELECT ROUND(price) FROM educbademo;
Output:
Rounding the Decimal Numbers
Instead of integer values, we will round the numbers to a particular decimal number with certain 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 the statement should be as follows –
Code:
SELECT ROUND(985.561,2);
That will result in the following output because as the decimal digit after two points, 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 –
Code:
SELECT ROUND(985.566,2);
That gives the following output with a value of 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.
Output:
Now, let us round the values of the certain column to decimal values using the round function. Let’s consider the table educbademo and round its price column to two decimal points. For this, the query statement will be as follows –
Code:
SELECT ROUND(price,2) FROM educbademo;
Output:
When rounding a number to two digits, increase the decimal value at the second place by one for numbers that are greater than or equal to 5, and keep it the same for all other numbers. If we round the column values to 3 digits, then the query statement will be as follows –
Code:
SELECT ROUND(price,3) FROM educbademo;
Output:
If we round the column values to 4 digits, then the query statement will be as follows –
Code:
SELECT ROUND(price,4) FROM educbademo;
Output:
We can see that 0 is appended at the end of the numeric value if the decimal value doesn’t contain any value in that decimal place.
Conclusion
The Round() function is used in the 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. If the second parameter is not specified, it is considered zero, and the number is converted to an integer value.
You can use this function directly on numbers or on the numeric values stored in the columns of a database table. The rounded value depends on the value of the digit immediately following the digit to be rounded. If the digit being rounded is greater than or equal to 5, increase the value of the digit by one.
Recommended Articles
We hope that this EDUCBA information on “PostgreSQL round” was beneficial to you. You can view EDUCBA’s recommended articles for more information.