How to initialize a BigDecimal with a very precise floating point value
java
Solution
You need to use a constructor that takes a `String` because the value of `double` constant is rounded by the compiler well before it gets to the `BigDecimal`'s constructor. In other words, the compiler sees your `999999999999999.9999` constant, and converts it to `double`. The `double` type does not have enough precision to store all the nines, so the value gets rounded to `1+E15`, and that's the value that gets passed to `BigDecimal`'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 `BigDecimal` do the interpretation of the sequence of nines, making sure that you get the exact precision that you need.
Problem
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.