question When I was writing a project function today, I had to do a statistical calculation of the amount of money, and then I needed to convert the units. So I wrote a statement similar to the following function, but the data I got was 4 decimal places, and normally we only need 2 places. Continue to search for data and perform precision conversion. After searching for a while, I am not satisfied with the data. Continue to test test When testing bugs and unknown situations, we must minimize the reproduction, streamline the test, and prevent other statements from interfering with the results.
And it runs on 3/4 devices, and the results are the same in different MySQL version environments. So we know that in MySQL, multiplication and division have different precision after the decimal point. I couldn't find suitable information in domestic forums, so I went to foreign forums to look for it, ask questions, and communicate. Answer First of all, I would like to thank other seniors for their answers and advice. We will also record the troubleshooting of the problem as detailed as possible. Civilizations survive because they have memory. I hope this article can help more friends.
test
Consistent with the conclusion of the appeal. Thanks to the foreign seniors for their guidance. Division uses 2 digits of precision If we insist on using division to solve our problem, we can use a function to convert the precision.
You can look up the parameters of DECIMAL on Baidu. Basically anyone who has created a table structure can understand it. @x and @y are the divisor and dividend. At the same time, I also wondered whether it is possible to set the default division precision in MySQL so that we don't have to use function calculations every time in SQL. Senior’s reply: If you don’t want unexpected situations to occur sometimes, you need to force type conversion every time. MySQL related documentation https://dev.mysql.com/doc/refman/8.0/en/arithmetic-functions.html
Precision rules for division From the literature cited above, we know that when two numbers are used for calculation, the precision of the result is determined by the precision of the first operand + the value of the system variable div_precision_increment. For example, the precision of 1 in our example is 0, and the system variable precision is 4 digits by default, so the result is 4 digits of precision.
So we can also change the default precision of division by modifying the default variables. Now we test
Summarize This is the end of this article about the inconsistent precision of MySQL multiplication and division, and four decimal places after division. For more relevant content about MySQL multiplication and division precision, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Let you understand the working principle of JavaScript
>>: Solution to the problem of session failure caused by nginx reverse proxy
Table of contents 1. Data Source 2. Overall ranki...
This article introduces the sample code of CSS to...
1. Download from the official website and unzip h...
1. InnoDB locking mechanism The InnoDB storage en...
Preface The essence of deadlock is resource compe...
Table of contents Purpose of the table For exampl...
By default, Docker runs over a non-networked UNIX...
I have just come into contact with and become fam...
Table of contents 1. How to locate and optimize s...
Table of contents Docker Installation Nvidia-dock...
1. Construction components 1. A form must contain...
The custom encapsulation code of the vue button c...
Table of contents Event-driven and publish-subscr...
CocosCreator version 2.3.4 Dragon bone animation ...
Table of contents 1. Lvs Introduction 2. Lvs load...