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
1. Introduction The EXPLAIN statement provides in...
After pressing Enter on the webpage, the form is a...
1. Command Introduction The cal (calendar) comman...
Introduction Part 1: Written at the beginning One...
This article example shares the specific code of ...
Preface I believe everyone has used the top comma...
This article example shares the specific code for...
The commands pulled by docker are stored in the /...
Table of contents 1. What is a closure? 2. The ro...
Today, I am sharing the valuable experience of a ...
Starting from MySQL 5.7, many security updates ha...
In real life, a lock is a tool we use when we wan...
** Detailed graphic instructions for installing y...
Table of contents use Use of EsLint Add a profile...
Join query A join query refers to a matching quer...