Float precision loss processing method

During the project development process, there are many input boxes related to the amount and payment type. The front-end input is passed to the back-end to receive. In many cases, it is directly received in Float or Double types. When the value is not very large, there is no problem, and the accuracy will not be wrong, but Generally, if the value is too large, the precision of the decimal point will be lost. The principle should be related to the underlying storage digits of the Float and double types. This article introduces how to deal with such problems in java!

The processing method is to use java.math.BigDecimal type parameters to receive in the background, do not use Float, Double types, BigDecimal will accurately receive the parameters passed by the front end, and it can be directly stored in the library!
In addition, when the back-end project involves quantity calculation and accumulation, the BigDecimal type must also be used, and the add method is used when adding, and finally the total value (BigDecimal type) is converted into a value!

Guess you like

Origin blog.csdn.net/wujian_csdn_csdn/article/details/106016173
Recommended