Updated June 9, 2023
Introduction to PostgreSQL Float Data Type
PostgreSQL provides different types of data types. The single table consists of a different column with different data types, and we need to store floating numbers that contain decimal points in the float column. Values are not approx., so at this condition, we use float data type. The float data type belongs under the numeric data type category. Float data type supports floating-point, real, and numeric numbers with 4 or 8 bytes numbers. In float data type, we use bit size, where bit size means the length of the string. For example, 3.4, 654.3, and -345.32 are the floating-point numbers. The system memory is limited; you can’t store numbers with infinite precision, so that is why we use float data type, which is the main purpose of the PostgreSQL Float data type.
Syntax and Parameters:
Consider the below syntax to understand how we can use the float data type.
1. float(p)
Column_name flaot
Explanation
Where float is the data type and p, this defines minimum acceptable binary digits, and it is accepted 4 or 8 bytes numbers. Where column name means specific column name in the created table, and float means float data type
2. real (num)
Explanation
Where real data type and num is number, and it is single precision.
3. numeric or numeric (p,s):
Explanation
Where Numeric is the data type and where p for digit and s for number after the decimal point is double precision.
How does Float Data Type work in PostgreSQL?
Double precision floating point decimal stored in float data type. Float data type corresponds to IEEE 4 byte floating to double floating-point. Generally, float data type stores scientific numbers, which can be calculated close to value. The number entered in the float data type can differ slightly because the float data type returns the most significant digit. This is a range of float data type 1E-307 to 1E+308.
Examples
Mainly there are three types of floating-point numbers in PostgreSQL, as below
1. Float (n):
In this type,, floating-point number have precision of at least n and up to a maximum of 8 bytes.
Example
Create table float_point (floatn float4 not null);
insert into float_point (floatn) values (543.23);
select * from float_point;
Explanation
In the above statement, we create a column with the name floatn in the float_point table, and its data type is float4; it shows the result in real. Illustrate the end result of the above declaration by using the use of the following snapshot.
The same example we write for float 8
create table float_point1 (floatn float8 not null);
insert into float_point1 (floatn) values (5434533453);
select * from float_point1;
Explanation
In the above statement, we create a column with the name floatn, and the data type is float in the float_point1 table; it shows a result in double precision. Illustrate the end result of the above declaration by using the use of the following snapshot.
2. real:
This is the second data type of float, and it is used as a 4-byte floating-point number. The real has a variety of at least 1E-37 to 1E+37 with an accuracy of up to 6 decimal digits.
Example
Create table float_point_r (real_no real not null);
insert into float_point_r (real_no) values (12345623);
select * from float_point_r;
Illustrate the end result of the above declaration by using the use of the following snapshot.
Another example of a real data type
CREATE TABLE emp (emp_id INTEGER PRIMARY KEY, emp_name TEXT, emp_age INTEGER, emp_weight REAL);
INSERT INTO emp (emp_id,emp_name,emp_age,emp_weight) VALUES (1, 'Alex', 26, 145.5), (2, 'John', 30, 150.3), (3, 'Bob', 34, 156.6);
select * from emp;
Explanation
In the above statement, emp is the table name, and we create a table with different data types like text integer and real data type for emp_weight.
Illustrate the end result of the above declaration by using the use of the following snapshot.
3. Numeric or numeric (p,s):
This is the third data type under the float, which means that it uses two notations for number representation, a real number with p and s where p for the digit and s for the number after the decimal point. The numeric(p,s) represents the exact number and is double precision. The range of double precision is 1E-307 to 1E+308, with an accuracy of at least fifteen digits.
Example
Create table float_point_numeric (numeric_no numeric(2,2) );
insert into float_point_numeric (numeric_no) values (0.3);
select * from float_point_numeric;
Illustrate the end result of the above declaration by using the use of the following snapshot.
Now we illustrate three data type in the following example
create table float_data_type
(floatn float8 not null, realn real not null, numerict numeric(3, 2));
insert into float_data_type (floatn,realn,numerict)values
(23456543355, 34564433, 3.22),
(87654354444, 45564333, 4.11);
select * from float_data_type;
Explanation
In the above statement, we execute all three types of float data type; in this example, we create a table with the name float_data_type and create different columns with different data types like column name floatn with float8 data type, realn with a real data type, and numeric with the numeric data type. Illustrate the end result of the above declaration by using the use of the following snapshot.
Example
If you want to store a large number of decimal digits, that time you require the correct data type, so PostgreSQL provides such a data type we call double precision. It is the numeric data type, using 8 bytes 0r up to 15 digits. Another name for double precision is float8.
create table test ( test_id SERIAL PRIMARY KEY, test_name VARCHAR(100), float_col double precision);
insert into test (test_id, test_name, float_col) VALUES (1, 'unit testing',12345678986), (2, 'fun_testing', 76548767523), (3, 'system_testing',0987923467);
select test_id, float_col from test;
Explanation
In the above statement, we implement a double-precision data type. Illustrate the end result of the above declaration by using the use of the following snapshot.
Conclusion
We hope you understand the PostgreSQL FLOAT data type from the above article. From the above article, we learn different types of float data types like float, real, and numeric with different examples; we also see double-precision examples. Finally, the main objective of this data type is to give correct number representation in tables and avoid the complexity of integer number representation.
Recommended Articles
We hope that this EDUCBA information on “PostgreSQL Float” was beneficial to you. You can view EDUCBA’s recommended articles for more information.