EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PostgreSQL Tutorial PostgreSQL round
Secondary Sidebar
PostgreSQL Tutorial
  • Basic
    • What is PostgreSQL
    • PostgreSQL Features
    • How to Install PostgreSQL
    • PostgreSQL Versions
    • PostgreSQL Architecture
    • PostgreSQL GUI
    • Postgres Command-Line
    • PostgreSQL Variables
    • PostgreSQL Data Types
    • PostgreSQL NOT NULL
    • PostgreSQL Integer
    • PostgreSQL Boolean
    • PostgreSQL BIGINT
    • PostgreSQL NULLIF
    • PostgreSQL Administration
    • PostgreSQL Commands
    • PostgreSQL Operators
    • PostgreSQL IN Operator
    • Postgres like query
    • PostgreSQL encode
    • PostgreSQL Cheat Sheet
    • PostgreSQL List Databases
    • PostgreSQL Rename Database
  • Control Statement
    • PostgreSQL IF Statement
    • PostgreSQL if else
    • PostgreSQL CASE Statement
    • PostgreSQL LOOP
    • PostgreSQL For Loop
    • PostgreSQL While Loop
  • Joins
    • Joins in PostgreSQL
    • PostgreSQL Inner Join
    • PostgreSQL Outer Join
    • LEFT OUTER JOIN in PostgreSQL
    • PostgreSQL FULL OUTER JOIN
    • PostgreSQL LEFT JOIN
    • PostgreSQL Full Join
    • PostgreSQL Cross Join
    • PostgreSQL NATURAL JOIN
    • PostgreSQL UPDATE JOIN
  • Queries
    • PostgreSQL Queries
    • PostgreSQL INSERT INTO
    • PostgreSQL WHERE Clause
    • PostgreSQL WITH Clause
    • PostgreSQL ORDER BY
    • PostgreSQL ORDER BY Random
    • PostgreSQL ORDER BY DESC
    • PostgreSQL GROUP BY
    • PostgreSQL group_concat
    • PostgreSQL HAVING
    • PostgreSQL Recursive Query
  • Advanced
    • PostgreSQL Schema
    • Postgres List Schemas
    • PostgreSQL Drop Schema
    • PostgreSQL VARCHAR
    • Array in PostgreSQL
    • PostgreSQL DDL
    • PostgreSQL List Users
    • Postgres Default User
    • Postgres add user
    • PostgreSQL User Password
    • PostgreSQL log_statement
    • PostgreSQL repository
    • PostgreSQL shared_buffer
    • PostgreSQL String Functions
    • PostgreSQL Compare Strings
    • PostgreSQL Text Search
    • PostgreSQL TEXT
    • PostgreSQL String Array
    • PostgreSQL where in array
    • PostgreSQL Constraints
    • PostgreSQL UNIQUE Constraint
    • PostgreSQL CHECK Constraint
    • PostgreSQL INTERSECT
    • PostgreSQL Like
    • Cursors in PostgreSQL
    • PostgreSQL UNION ALL
    • Indexes in PostgreSQL
    • PostgreSQL Index Types
    • PostgreSQL REINDEX
    • PostgreSQL UNIQUE Index
    • PostgreSQL Clustered Index
    • PostgreSQL DROP INDEX
    • PostgreSQL DISTINCT
    • PostgreSQL FETCH
    • PostgreSQL RAISE EXCEPTION
    • PostgreSQL Auto Increment
    • Sequence in PostgreSQL
    • Wildcards in PostgreSQL
    • PostgreSQL Subquery
    • PostgreSQL Alias
    • PostgreSQL LIMIT
    • PostgreSQL Limit Offset
    • PostgreSQL LAG()
    • PostgreSQL Table
    • Postgres Show Tables
    • PostgreSQL Describe Table
    • PostgreSQL Lock Table
    • PostgreSQL ALTER TABLE
    • Postgres Rename Table
    • PostgreSQL List Tables
    • PostgreSQL TRUNCATE TABLE
    • PostgreSQL Table Partitioning
    • Postgres DROP Table
    • PostgreSQL Functions
    • PostgreSQL Math Functions
    • PostgreSQL Window Functions
    • Aggregate Functions in PostgreSQL
    • PostgreSQL Primary Key
    • Foreign Key in PostgreSQL
    • PostgreSQL Procedures
    • PostgreSQL Stored Procedures
    • PostgreSQL Views
    • PostgreSQL Materialized Views
    • Postgres Create View
    • PostgreSQL Triggers
    • PostgreSQL DROP TRIGGER
    • PostgreSQL Date Functions
    • PostgreSQL TO_DATE()
    • PostgreSQL datediff
    • PostgreSQL Timestamp
    • PostgreSQL CURRENT_TIMESTAMP()
    • PostgreSQL Notify
    • PostgreSQL LENGTH()
    • PostgreSQL blob
    • PostgreSQL Median
    • PostgreSQL kill query
    • PostgreSQL Formatter
    • PostgreSQL RANK()
    • PostgreSQL Select
    • PostgreSQL Average
    • PostgreSQL DATE_PART()
    • PostgreSQL EXECUTE
    • PostgreSQL COALESCE
    • PostgreSQL EXTRACT()
    • PostgreSQL Sort
    • PostgreSQL TO_CHAR
    • PostgreSQL Interval
    • PostgreSQL Number Types
    • PostgreSQL ROW_NUMBER
    • Alter Column in PostgreSQL
    • PostgreSQL Identity Column
    • PostgreSQL SPLIT_PART()
    • PostgreSQL CONCAT()
    • PostgreSQL replace
    • PostgreSQL TRIM()
    • PostgreSQL MAX
    • PostgreSQL DELETE
    • PostgreSQL Float
    • PostgreSQL OID
    • PostgreSQL log
    • PostgreSQL REGEXP_MATCHES()
    • PostgreSQL MD5 
    • PostgreSQL NOW()
    • PostgreSQL RANDOM
    • PostgreSQL round
    • PostgreSQL Trunc()
    • PostgreSQL TIME
    • PostgreSQL IS NULL
    • PostgreSQL CURRENT_TIME
    • PostgreSQL MOD()
    • Postgresql Count
    • PostgreSQL Datetime
    • PostgreSQL MIN()
    • PostgreSQL age()
    • PostgreSQL enum
    • PostgreSQL OR
    • PostgreSQL Wal
    • PostgreSQL NOT IN
    • PostgreSQL SET
    • PostgreSQL Current Date
    • PostgreSQL Compare Date
    • PostgreSQL SERIAL
    • PostgreSQL UUID
    • PostgreSQL Merge
    • PostgreSQL Database
    • PostgreSQL Clone Database
    • PostgreSQL Copy Database
    • PostgreSQL Show Databases
    • PostgreSQL Restore Database
    • PostgreSQL DROP DATABASE
    • PostgreSQL ALTER DATABASE
    • Postgres DROP Database
    • Postgres Dump Database
    • PostgreSQL OFFSET
    • PostgreSQL GRANT
    • PostgreSQL COMMIT
    • PostgreSQL ROLLUP
    • PostgreSQL JSON
    • EXPLAIN ANALYZE in PostgreSQL
    • PostgreSQL Temporary Table
    • PostgreSQL Show Tables
    • PostgreSQL cluster
    • PostgreSQL Replication
    • PostgreSQL Logical Replication
    • PostgreSQL flush privileges
    • PostgreSQL Tablespaces
    • CAST in PostgreSQL
    • PostgreSQL CTE
    • hstore in PostgreSQL
    • PostgreSQL Encryption
    • PostgreSQL DECODE()
    • PostgreSQL Vacuum
    • PostgreSQL EXCLUDE
    • Postgres Change Password
    • Postgres Delete Cascade
    • PostgreSQL EXCEPT
    • PostgreSQL Roles
    • PostgreSQL Link
    • PostgreSQL Partition
    • PostgreSQL column does not exist
    • PostgreSQL Log Queries
    • PostgreSQL escape single quote
    • PostgreSQL Query Optimization
    • PostgreSQL Character Varying
    • PostgreSQL Transaction
    • PostgreSQL Extensions
    • PostgreSQL Import CSV
    • PostgreSQL Client
    • PostgreSQL caching
    • PostgreSQL Incremental Backup
    • PostgreSQL JSON vs JSONNB
    • PostgreSQL JDBC Driver
    • PostgreSQL Interview Questions

PostgreSQL round

By Priya PedamkarPriya Pedamkar

PostgreSQL round

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:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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:

numeric value

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:

number say 98.536

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:

educbademo

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:

query statements

let us confirm the contents of the table by using the following SELECT query –

Code:

SELECT * FROM educbademo;

Output:

SELECT query

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:

round() function

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:

PostgreSQL round - 7

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:

PostgreSQL round - 8

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:

PostgreSQL round - 9

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:

PostgreSQL round - 10

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 –

  1. PostgreSQL Notify
  2. PostgreSQL JSON
  3. PostgreSQL Schema
  4. PostgreSQL NATURAL JOIN
Popular Course in this category
PostgreSQL Course (2 Courses, 1 Project)
  2 Online Courses |  1 Hands-on Project |  7+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more