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. 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, mysql> insert into t1 values(1.2345,1.2345,1.2345); mysql> show warnings; 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:
|
<<: How to mount a data disk on Tencent Cloud Server Centos
>>: Detailed explanation of Nest.js environment variable configuration and serialization
background Before starting the article, let’s bri...
Preface As Linux operation and maintenance engine...
1. Radio grouping As long as the name is the same,...
1. Business scenario introduction Suppose there i...
Table of contents 1. Introduction 2. Create a Vit...
I recently watched a video of a foreign guy using...
Table of contents 1. Basic usage and logic 2. Fea...
Preface In the springboot configuration file, the...
This article describes the usage of MySQL stored ...
This article shares the specific code for impleme...
CSS attribute selectors are amazing. They can hel...
The command format for mysql login is: mysql -h [...
Table of contents 1. Decoupled assignment of arra...
Table of contents Set a not null constraint when ...
MySQL stored procedures, yes, look like very rare...