java – Bigdecimal precision loss

Question:

A variable is created BigDecimal bet = new BigDecimal(0.00000010) and passed to the server and it returns a different value than expected. It is not clear why this is so.

Answer:

When using a constructor that takes double as input, an unpleasant feature occurs, noted in the documentation.

Since a real number, when converted to binary form, is represented, as a rule, by an infinite binary fraction, then when creating an object, for example, BigDecimal(0.1) , the mantissa stored in the object will turn out to be very large. ( link to article )

Therefore, the number will be stored inaccurately. In this regard, it is better to use a constructor that takes a string as input:

Bigdecimal bet = new Bigdecimal("0.00000010"); 
Scroll to Top
AllEscort