Talking about how to control the accuracy from the bug of the withdrawal amount of the micro loan network

 
post
 
 
 
 

Talking about how to control the accuracy from the bug of the withdrawal amount of the micro loan network

 Code Farm Tour World  2017-10-19 09:23

This morning, I went to Weidai.com to withdraw money, and I entered the withdrawal interface and found such a problem:

Bug in the withdrawal page of Weidai.com

I see, the amount I withdraw has n more than 0 after the decimal point. I don't know what the technical framework system of Weidai.com is, nor what the development language is. If it is java, I need to popularize java for everyone. How to control the accuracy inside.

A BigDecimal class is used in java to control the precision. You can check how to use it by yourself.

Common operations are as follows:

Addition, subtraction, multiplication and division are as follows:

add(BigDecimal augend)

subtract(BigDecimal subtrahend)

multiply(BigDecimal multiplicand)

divide(BigDecimal divisor)

This question is also a common interview question. In the financial industry, the BigDecimal class is often used to encapsulate common amount operations, such as precision control, such as the conversion of points and yuan.

If you use double, float, etc. for amount calculation, you will find that you will not get the results you want, and may even get different results in different scenarios.

This is mentioned in the Alibaba java development protocol:

[Mandatory] The decimal type is decimal, and the use of float and double is prohibited.

Note: When float and double are stored, there is a problem of precision loss, and it is very likely that incorrect results will be obtained when comparing values. If the range of stored data exceeds the range of decimal, it is recommended to split the data into integers and decimals and store them separately

Guess you like

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