Literal decimal floating-point numbers cannot always be precisely represented as an IEEE 754 floating-point value. Consequently, the
BigDecimal(double val) constructor must not be passed a floating-point literal as an argument when doing so results in an unacceptable loss of precision.
Noncompliant Code Example
This noncompliant code example passes a
double value to the
BigDecimal constructor. Because the decimal literal
0.1 cannot be precisely represented by a
double, precision of the
BigDecimal is affected.
This compliant solution passes the decimal literal as a
String so that the
BigDecimal(String val) constructor is invoked and the precision is preserved:
BigDecimal(double val) constructor with decimal floating-point literals can lead to loss of precision.
Automated detection is straightforward.
|CERT.NUM10.BBDCC||Do not pass floating point values to the 'BigDecimal' constructor|
|S2111||"BigDecimal(double)" should not be used|