A BigDecimal is made from a floating-point literal it cannot hold exactly
What it finds
new BigDecimal(0.1) is the exact value of the double nearest to 0.1, which is 0.1000000000000000055511151231257827..., not 0.1. new BigDecimal("0.1") and BigDecimal.valueOf(0.1) are 0.1.
Only literals whose value a double, or a float for literals with an f, cannot hold exactly are reported: new BigDecimal(0.5) is exact. The fix writes the literal as a string, which changes the value to the one written, so it is offered as unsafe.
From the rule's source, inexact_bigdecimal_literal.rs.
import java.math.BigDecimal;import java.math.MathContext; class Money { BigDecimal[] values() { return new BigDecimal[] { new BigDecimal(0.1),tx0 reports: 0.1 is not exactly representable new BigDecimal(-2.675, MathContext.DECIMAL64),tx0 reports: 2.675 is not exactly representable new BigDecimal(0.1f),tx0 reports: 0.1f is not exactly representable new BigDecimal(1e-3),tx0 reports: 1e-3 is not exactly representable new BigDecimal(0.5), new BigDecimal(1.25e2), new BigDecimal(100.0), new BigDecimal("0.1"), new BigDecimal(1), BigDecimal.valueOf(0.1), }; }} Turn it on, off or suppress it
The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:
[rules]
"tx0/java:inexact-bigdecimal-literal" = "off"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/java:inexact-bigdecimal-literal] reason="..."tx0 explain tx0/java:inexact-bigdecimal-literal shows the same in the terminal. Found a false positive? Report it.