Float and double types have errors and are suitable for scientific or engineering computing, such as commercial applications, especially shopping and billing systems. They must use bigdecimal for accuracy.
For example, if we define a double value as 0.01 and it may be printed or calculated in the future, it is actually a value like 0.010000000003456. If we use this value for calculation, as the number of computations increases and the number of double numbers increases, the error will increase, which is not allowed in commercial computing. For example, 10 yuan may be 9.999999999999348 yuan in our program. In this way, this person cannot buy 10 yuan.
What's more, float and double may sometimes become the representation of scientific and technological methods, which is also annoying. For example, in our easycluster, after the computing task is completed, the backend consumes the task datagram. The data is in string format, and then the EC converts the data to float, then, the calculated value is stored in the database. But there is a problem. The first problem is the error mentioned above. This is not to mention. The second problem is that some tasks are wrong tasks. Once they are run, they disappear immediately, therefore, the consumption data of such tasks is very small. For example, when the CPU is running for 0.000001 seconds, after the data becomes float, EC performs a short operation (because the number of decimal places is too long, EC only keeps the four digits after the decimal point, and does Java provide such a ready-made rounding function, so I wrote a truncstring function, which accepts a string and truncates it to four digits after the decimal point, because 0.000001 is converted to float, it is actually 1.00000e-6. After this float is changed to string, it is also 1.00000e-6. In this phase, the float number is immediately changed to 1, which is increased by 1000000 times !!
All in all, using float or double for exact calculation is a big problem. We need to use bigdecimal. For more information, see the attachment. Some examples have code.
The most critical point is to use the new bigdecimal (string input) constructor, because the value produced by this constructor is accurate, that is, the value imported from "0.1" is 0.1, it won't be something like 0.10000000456. As a result, the document explains, in the database, data should also be stored as strings, so that the data can be taken out of the database and converted directly into exact values using bigdecimal, the addition, subtraction, multiplication, division, rounding, and remainder operations are precise.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.