Decimal vs Double vs Float in MySQL
Last Updated :
21 Jun, 2024
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.
Decimal In MySQL
- Decimal datatype is used to store the fixed precision datatypes which have the exact values.
- Decimal datatypes are used where fixed precision is required such as storing quantities, percentages, and prices.
Example
Let's create a table student with Roll_number, name, and Marks.
CREATE TABLE students (
student_id INT PRIMARY KEY,
name VARCHAR(255),
percentage DECIMAL(5, 2)
);
- Here we declared the decimal where (5,2) represents the (number of total digits, digits after decimals). So here we assume the percentage is out of 100 so the total digits will be 5 and 2 digits are allowed after decimal.
Let's insert the values into the table:
INSERT INTO students (student_id, name, percentage) VALUES
(1, 'Kartik', 85.50),
(2, 'Roshan', 72.25),
(3, 'Athul', 91.75);
To display the students table we will use the following MySQL query
select * from students
Table Students:
+------------+--------+------------+
| student_id | name | percentage |
+------------+--------+------------+
| 1 | Kartik | 85.50 |
| 2 | Roshan | 72.25 |
| 3 | Athul | 91.75 |
+------------+--------+------------+
Here we can see that the percentages are in decimal data type where the 2 digits are present after the decimal.
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 this:
| 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.
Double in MySQL
- Double is used for the floating-point data type which stores the approximate numeric values.
- Double uses the 8 bytes i.e. 64 bits to store the values. So it has a wide range of values. Double is more precise than the Float.
- Double provides the precision up to the 15 decimal points.
Example
Let's create a table with the double data type
CREATE TABLE students2 (
id INT PRIMARY KEY,
name VARCHAR(255),
value DOUBLE
);
Here value has the data type double used.
Let's insert the values in the table
INSERT INTO students2 (id, name, value) VALUES
(1, 'Kartik', 60.3),
(2, 'Roshan', 50.45),
(3, 'Athul', 70.21);
To display the students2 table we will use the following MySQL query
select * from students2
Table students2:
+----+--------+-------+
| id | name | value |
+----+--------+-------+
| 1 | Kartik | 60.3 |
| 2 | Roshan | 50.45 |
| 3 | Athul | 70.21 |
+----+--------+-------+
- Here we can see that the value is stored in the Double format. But here a question will arise what is the difference between decimal data type and double?
- 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
SELECT id, name, value, value / 3 AS divided_by_3
FROM students2;
The students2 table will look like
+----+--------+-------+--------------------+
| id | name | value | divided_by_3 |
+----+--------+-------+--------------------+
| 1 | Kartik | 60.3 | 20.099999999999998 |
| 2 | Roshan | 50.45 | 16.816666666666666 |
| 3 | Athul | 70.21 | 23.403333333333332 |
+----+--------+-------+--------------------+
Here if we notice the divided_by_3 column, the values are rounded off to the nearest value. This is the difference between the Float and Double data types. In Double data types values have a wide range as shown in the above table as values are more precise than that of float data type.
Decimal vs Double vs Float in MySQL
Here's a comparison of Decimal, Double, and Float data types in MySQL presented in tabular form:
Data Type | Precision | Storage Size | Suitable For |
---|
Decimal | Exact | Variable | Financial Data |
Double | Approximate | 8 bytes | General Purpose |
Float | Approximate | 4 bytes | Scientific Data |
Conclusion
So here we noticed the difference between the decimal, float, and double data types. We also implemented the all data types in MySQL workbench using the SQL query. The main difference between these data types is the precision available after the decimal points. Decimal offers exact precision for financial data, while Double provides higher precision than Float, making it suitable for general-purpose use. Float, with lower precision, is often preferred for scientific computations.
Similar Reads
MySQL Float vs Decimal
In MySQL, when dealing with numbers, you have two main choices: FLOAT and DECIMAL. Each has its unique strengths and best uses. Picking the right one is vital for accurate and efficient data handling in your database. Understanding their differences helps you make smart decisions for better performa
4 min read
How to Insert Double and Float Values in SQLite?
SQLite, a lightweight and server-less relational database management system, provides powerful features for handling various data types, including double and float values. In this article, we will explore various methods along with examples for inserting double and float values into SQLite tables, e
3 min read
SQL Query to Remove Decimal Values
In SQL, decimal values are often used to represent floating-point numbers. However, there are instances where we might need to remove decimal places or adjust the number of decimals in your queries. In this article explains various methods in SQL to remove or round off decimal values, including ROUN
3 min read
Difference Between Decimal and Float in PL/SQL
Have you ever thought about the differences between Decimal and Float in PL/SQL? These two numeric data types play important roles in database programming that offer unique features for handling numeric values. In this article, we'll explore What are Decimals and Float along with their syntax, usage
2 min read
C Float and Double
Float and double are two primitive data types in C programming that are used to store decimal values. They both store floating point numbers but they differ in the level of precision to which they can store the values.In this article, we will study each of them in detail, their memory representation
5 min read
Difference between Decimal, Float and Double in .Net
Float : It is a floating binary point type variable. Which means it represents a number in it's binary form. Float is a single precision 32 bits(6-9 significant figures) data type. It is used mostly in graphic libraries because of very high demand for processing power, and also in conditions where r
2 min read
DATETIME vs TIMESTAMP Data Type in MySQL?
When designing a database schema in MySQL, choosing the appropriate data type for storing date and time information is important. MySQL offers two primary data types for this purpose DATETIME and TIMESTAMP. Each has its characteristics and use cases and understanding these can help us make the right
6 min read
Practice Questions on Decimals
Decimal is a numerical representation that uses a dot, which we call a decimal point, to separate the whole number part from its fractional part. The decimal numeral system is used as the standard system that is used to distinguish integer and non-integer numbers. In this series of practice question
8 min read
Cannot Convert String To Float in Python
Python, a versatile and powerful programming language, is widely used for data manipulation and analysis. However, developers often encounter challenges, one of which is the "Cannot Convert String To Float" error. This error occurs when attempting to convert a string to a float, but the string's con
3 min read
Convert String with Comma To Float in Python
When working with data in Python, it's not uncommon to encounter numeric values formatted with a mix of commas and dots as separators. Converting such strings to float is a common task, and Python offers several simple methods to achieve this. In this article, we will explore five generally used met
3 min read