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 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.
Syntax:
returned_value = ROUND (source_value [ , decimal_count ] )
Where the two parameters carry the following meaning –
- source_value: The numeric value or numeric expression needs to be rounded to integer or the particular number of decimal points to maintain precision.
- decimal_count: The optional parameter 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 –
Code:
SELECT ROUND(45.145);
Output:
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
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 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
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 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 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 –
Code:
SELECT ROUND(985.566,2);
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.
Output:
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 –
Code:
SELECT ROUND(price,2) FROM educbademo;
Output:
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 –
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:
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.
Conclusion
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 table columns in the database. The rounded value depends on the digit’s value just after the place, after which the value is to be rounded. If it is greater than or equal to 5, then the digit’s value up to which the rounding is being made is increased by one.
Recommended Articles
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 –