This article uses examples to illustrate the pitfalls of rounding operations using the ROUND function in MySQL. Share with you for your reference, the details are as follows: In MySQL, Problem DescriptionSuppose we have a data table test, the table creation statement is as follows CREATE TABLE test ( id int(11) NOT NULL AUTO_INCREMENT, field1 bigint(10) DEFAULT NULL, field2 decimal(10,0) DEFAULT NULL, field3 int(10) DEFAULT NULL, field4 float(15,4) DEFAULT NULL, field5 float(15,4) DEFAULT NULL, field6 float(15,4) DEFAULT NULL, PRIMARY KEY (id) )ENGINE=InnoDB DEFAULT CHARSET=utf8; We created a table named test, which contains multiple fields besides the id field, with different data types. Let's insert a piece of data into this table. INSERT INTO test (field1, field2, field3, field4, field5, field6) VALUE (100, 100, 100, 1.005, 3.5, 2.5); After inserting, the data in the table looks like this mysql> select * from test; +----+--------+--------+--------+--------+--------+--------+ | id | field1 | field2 | field3 | field4 | field5 | field6 | +----+--------+--------+--------+--------+--------+--------+ | 1 | 100 | 100 | 100 | 1.0050 | 3.5000 | 2.5000 | +----+--------+--------+--------+--------+--------+--------+ 1 row in set (0.00 sec) If we execute the following SQL now, what do you think the result will be? SELECT round(field1 * field4), round(field2 * field4), round(field3 * field4), round(field1 * 1.005), round(field2 * 1.005), round(field3 * 1.005), round(field5), round(field6) FROM test; At first, I thought the result would be 101, because the six values above are all rounded to 100 * 1.005, so the result must be 101, and the last two must be 4 and 3. However, the final result is quite different from what I expected. *************************** 1. row *************************** round(field1 * field4): 100 round(field2 * field4): 100 round(field3 * field4): 100 round(field1 * 1.005): 101 round(field2 * 1.005): 101 round(field3 * 1.005): 101 round(field5): 4 round(field6): 2 1 row in set (0.00 sec) Why is this happening?It is the same 100*1.005, why is the result obtained by multiplying the fields in the database different from that obtained by directly multiplying the field and the decimal? I have no idea how to solve this problem, and I have searched Baidu and Google but to no avail. . . There is no other way but to rely on myself. The most useful thing at this time is the official website documentation. So I searched the official MySQL documentation about the ROUND function, which contains the following two rules:
From these two rules, we can see that when we multiply two fields, the final result is processed according to the float type, and the float type is not an exact number in the computer, so the processing result will be processed according to the second rule. The result of the direct integer field and the decimal operation such as 1.005 is because the two values involved in the operation are exact numbers, so it is calculated according to the first rule. From the results of executing Summarize From this example, we can see that we need to be very careful when using Readers who are interested in more MySQL-related content can check out the following topics on this site: "MySQL query skills", "MySQL transaction operation skills", "MySQL stored procedure skills", "MySQL database lock related skills summary" and "MySQL common function summary" I hope this article will be helpful to everyone's MySQL database design. You may also be interested in:
|
<<: Example of Vue transition to achieve like animation effect
>>: Simple implementation method of Linux process monitoring and automatic restart
1.0 Redis persistence Redis is an in-memory datab...
Table of contents Background 1. Thought Analysis ...
I worked in operations and maintenance for two ye...
This article mainly introduces how to implement a...
1. Mycat application scenarios Mycat has been dev...
today select * from table name where to_days(time...
<br />User experience is increasingly valued...
1. In the previous chapter, we learned that we ca...
Table of contents 1. Add attributes 2. Merge mult...
Copy code The code is as follows: <object clas...
When using docker, you may find that the file can...
If a div sets background-color and background-ima...
This article mainly explains how to install the M...
1. Download the tomcat image docker pull tomcat:8...
Installation of Python 3 1. Install dependent env...