I have a database column with Decimal(19,5) size, I want a BigDecimal in java to store the value but if I write
BigDecimal(999999999999999.9999);
the value is rounded to 1+E15 which I don’t want it to be,So how can I get the complete precision of the number.
One way I know is using
BigDecimal("999999999999999.9999");
I thought there should be a better option to specify the precision.
You need to use a constructor that takes a
Stringbecause the value ofdoubleconstant is rounded by the compiler well before it gets to theBigDecimal‘s constructor. In other words, the compiler sees your999999999999999.9999constant, and converts it todouble. Thedoubletype does not have enough precision to store all the nines, so the value gets rounded to1+E15, and that’s the value that gets passed toBigDecimal‘s constructor. The precision is gone before the program gets to execute its first instruction.On the other hand, when you pass a string, you let
BigDecimaldo the interpretation of the sequence of nines, making sure that you get the exact precision that you need.