Problem solving process encountered in the project

first amount calculation

1 Floating-point number operations have always been known to me to be inaccurate. Using double for amount calculation will inevitably lead to inaccurate calculation results.

Now the project needs to do a module of reimbursement form, which involves the calculation of the amount. float 8 bits, double 17 bits or so, more than this number, calculated

Results are unpredictable. Therefore, I use the BigDecimal operation class in Java in the background. There are many traps in the source code in the process of using BigDecimal, which will not be described one by one. The background ensures the accuracy of the calculation. What about the correctness of the front-end Js operation? The front desk parseFloat and parseInt have a limited number of digits. About 5 or 6 digits cannot be expressed. At this time, when you search for Number from the Internet, the number of digits that can be expressed reaches 10 digits, which is enough. According to the principle and mechanism of floating-point numbers, when JS calculates, first convert double to String, shift to Number, calculate, and then move the decimal place to convert to floating-point number.

If it exceeds the specified number of digits, it has to use the large number operation of Js, which is the same as the principle of BigDecimal. Then searched the tool code for Js large number operation from the Internet. Copy it over and use it directly, and you're done. 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326173475&siteId=291194637