DOUBLE PRECISION data type stores numbers having a fractional part. The data type DOUBLE PRECISION are variable precision data type. Their precision may vary depending on the input values. This data type can be used where the accurate/exact number is not needed.
What is PostgreSQL DOUBLE PRECISION Data Type?
One of the most significant data types among all the data types in Postgres is the DOUBLE PRECISION data type. This data type offers memory storage of an 8-byte floating-point number. The number that contains the fractional part is stored in the DOUBLE PRECISION data type.
These data types offer a precision range of 15-17 digits which means it supports a very high precision. Let's see what the syntax of double precision type looks like:
col_name DOUBLE PRECISION
The syntax for double precision is quite simple. The name of the column is specified before the DOUBLE PRECISION data type.
Let’s move toward an example that will make the concept of DOUBLE PRECISIONS more clear.
Example
Let’s create a table “Students_scholarships” with a column “Scholarship_amount” having a DOUBLE PRECISION data type. The query for the table is:
CREATE TABLE Students_scholarships ( Student_id SERIAL PRIMARY KEY, Student_name VARCHAR(100) NOT NULL, Scholarship_amount DOUBLE PRECISION NOT NULL );
A table will successfully be created.
We will now insert some data into the table:
INSERT INTO students_scholarships (student_name, scholarship_amount) VALUES ('Katherine John', 14050.70), ('Williams Smith', 28440.85), ('Alex Peter', 175350.62);
The table with the above values will be created as follows:
We can also use the ROUND function to round off the scholarship amount as it will not create a huge difference in the amount. The query will be as follows:
SELECT Student_name, ROUND(scholarship_amount) as rounded_scholarship_Amount FROM students_scholarships;
The output for the query will give the rounded scholarship amount:
So this was all about the DOUBLE PRECISION data type.
Conclusion
Understanding data types is important in PostgreSQL. It greatly influences the fact that our database will be interacting with the application program. DOUBLE PRECISION data type offers high precision and wide storage space of 8 bytes. This data type is widely used where accurate numbers are not of very high importance. Its applications include; finances, modern engineering, and scientific computations.