Literal decimal floating-point numbers cannot always be represented precisely using the double primitive type because the underlying representation of double is binary. This imprecision becomes apparent when a BigDecimal is constructed from a double. precisely represented as an IEEE 754 floating point value. Consequently, the BigDecimal(double val) constructor must not be invoked with passed a floating-point literalsliteral as an argument when doing so results in an unacceptable loss of precision.
Noncompliant Code Example
...
This compliant solution passes the decimal literal as a String so that the BigDecimal(String val) constructor is invoked , and the precision is preserved.
| Code Block | ||
|---|---|---|
| ||
// prints 0.1
System.out.println(new BigDecimal("0.1"));
|
...