The difference between Decimal type and Float Double in MySQL (detailed explanation)

The difference between Decimal type and Float Double in MySQL (detailed explanation)

MySQL has non-standard data types such as float and double, as well as standard data types such as decimal.

The difference is that non-standard types such as float and double store approximate values ​​in the DB, while Decimal stores numerical values ​​in the form of strings.
Float and double types can store floating-point numbers (i.e. decimal types), but float has a disadvantage. When the data you give is an integer, it will be treated as an integer. In this way, we naturally encounter problems when storing and accessing currency values. My default value is: 0.00, but the actual storage is 0. Similarly, when I deposit and access currency as 12.00, the actual storage is 12.

Fortunately, MySQL provides two data types: decimal, which can easily solve the above problem: the decimal type is implemented by MySQL in the same type, which is allowed in the SQL92 standard. They are used to store values ​​where exact precision is important, such as data related to money.

Data Definition

float(M,S) M is the total length and S is the length after the decimal point. There are many inaccurate examples on the Internet, copy them as follows:

mysql> create table t1(c1 float(10,2), c3 decimal(10,2));

Query OK, 0 rows affected (0.02 sec)

mysql> insert into t1 values(9876543.21, 9876543.12);

Query OK, 1 row affected (0.00 sec)

mysql> select * from t1;

+----------------+-----------------+

| c1 | c3 |

+----------------+-----------------+

| 9876543.00 | 9876543.12 |

+----------------+------------------+

2 rows in set (0.00 sec)

Another example: DECIMAL(5,2)

mysql> create table t1(id1 float(5,2) default null,id2 double(5,2) default null,
id3 decimal(5,2) default null );

mysql> insert into t1 values(1.2345,1.2345,1.2345);
Query OK, 1 row affected, 1 warning (0.04 sec)

mysql> show warnings;
+-------+------+------------------------------------------+
| Level | Code | Message |
+-------+------+------------------------------------------+
| Note | 1265 | Data truncated for column 'id3' at row 1 |
+-------+------+------------------------------------------+
1 row in set (0.00 sec)

1.2345 --- There are at most 2 decimal places, so it can be saved, and the data will be automatically rounded off, but waning will be reported

12.34 --- OK

1234.5 --- Because the decimal part is less than 2 digits, it needs to be padded with 0. So it should be saved as 1234.50. So the total number of digits exceeds 5, and an error is reported when saving.

1.2 --- Fill the decimal part with 0. Save as per 1.20.

Default state comparison

If the longitude and scale are not written for floating-point numbers, they will be saved according to the actual precision value. If the precision and scale are written, the rounded result will be automatically inserted and the system will not report an error. If the precision and scale are not written for fixed-point numbers, the default value of decimal(10,0) will be used. If the data exceeds the precision and scale value, the system will report an error.

The above article on the difference between Decimal type and Float Double in MySQL (detailed explanation) is all the content that the editor shares with you. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • A brief introduction to the usage of decimal type in MySQL
  • Detailed explanation of the meaning of N and M in the MySQL data type DECIMAL(N,M)
  • Method to convert scientific notation numeric string to decimal type
  • Database data type float to C# type decimal, float data type conversion is invalid
  • Detailed explanation of the usage of DECIMAL in MySQL data type
  • Implementation of mysql decimal data type conversion
  • Detailed explanation of the decimal padding problem of decimal data type in MySQL
  • Detailed explanation of the usage of MySQL data type DECIMAL
  • In-depth explanation of the use and implementation of the Decimal type in the database

<<:  How to mount a data disk on Tencent Cloud Server Centos

>>:  Detailed explanation of Nest.js environment variable configuration and serialization

Recommend

CSS to achieve Cyberpunk 2077 style visual effects in a few steps

background Before starting the article, let’s bri...

Troubleshooting ideas and solutions for high CPU usage in Linux systems

Preface As Linux operation and maintenance engine...

MySQL sharding details

1. Business scenario introduction Suppose there i...

Vite+Electron to quickly build VUE3 desktop applications

Table of contents 1. Introduction 2. Create a Vit...

Detailed explanation of CSS3 to achieve responsive accordion effect

I recently watched a video of a foreign guy using...

Detailed explanation of JavaScript error capture

Table of contents 1. Basic usage and logic 2. Fea...

Linux editing start, stop and restart springboot jar package script example

Preface In the springboot configuration file, the...

Example analysis of mysql stored procedure usage

This article describes the usage of MySQL stored ...

Implementing a simple Gobang game with native JavaScript

This article shares the specific code for impleme...

How to use CSS attribute selectors to splice HTML DNA

CSS attribute selectors are amazing. They can hel...

MySQL login and exit command format

The command format for mysql login is: mysql -h [...

JS ES new feature of variable decoupling assignment

Table of contents 1. Decoupled assignment of arra...

MySQL not null constraint case explanation

Table of contents Set a not null constraint when ...

Summary of MySQL stored procedure permission issues

MySQL stored procedures, yes, look like very rare...