FLOAT in MySQL
- Float is also a floating data type that is used to store the approximate values.
- Float uses the 4 bytes i.e. 32 bits to store the values. So it has a wide range of values. But it is not always precise.
- Float provides the precision up to the 8 decimal points.
Example
Let’s create a table with the float data type
CREATE TABLE students1 (
id INT PRIMARY KEY,
name VARCHAR(255),
value FLOAT
);
- Here value has the data type float used.
Let’s insert the values in the table
INSERT INTO students1 (id, name, value) VALUES
(1, 'Kartik', 50.3),
(2, 'Roshan', 60.45),
(3, 'Athul', 70.21);
To display the students1 table we will use the following MySQL query
select * from students1
Table students1:
+----+--------+-------+
| id | name | value |
+----+--------+-------+
| 1 | Kartik | 50.3 |
| 2 | Roshan | 60.45 |
| 3 | Athul | 70.21 |
+----+--------+-------+
- Here we can see that the value is stored in the Float format. But here a question will arise what is the difference between decimal data type and float data type?
- To understand it, we will create a new column that will have the arithmetic operation of value divided by 3.
The query for the given condition is
ALTER TABLE students1 ADD COLUMN value_divided_by_3_d Double;
UPDATE students1 SET value_divided_by_3_d = 100 / 3;
The students1 table will look like:
| id | name | value | value_divided_by_3_d |
|----|---------|---------|----------------------|
| 1 | Kartik | 50.3 | 33.333333333333336 |
| 2 | Roshan | 60.45 | 33.333333333333336 |
| 3 | Athul | 70.21 | 33.333333333333336 |
Here if we notice the divided_by_3 column, the values are rounded off to the nearest value. This is the difference between the decimal and Float data types. In Float data types values have a wide range as shown in the above table which gives more precision than the Decimal.
Decimal vs Double vs Float in MySQL
In MySQL, Decimal, Double, and Float are numeric data types used to represent different precision levels for numerical values. Decimal offers precise numeric storage with fixed-point arithmetic, suitable for financial data.
Double and Float are approximate numeric types, where Double provides higher precision than Float. Float is typically used for scientific calculations or where precision isn’t critical, while Double strikes a balance between precision and storage efficiency.
Contact Us