Files
openide/java/java-impl/resources/inspectionDescriptions/UnpredictableBigDecimalConstructorCall.html
Leonid Shalupov 40795fe787 IJI-2422: community/java: move resources under resources root
GitOrigin-RevId: 8b2b63fc6db476ca0c2cfe5cadd84db6c4236d0f
2025-02-05 04:43:28 +00:00

29 lines
1.1 KiB
HTML

<html>
<body>
Reports calls to <code>BigDecimal</code> constructors that accept a <code>double</code> value.
These constructors produce <code>BigDecimal</code> that is exactly equal to the supplied <code>double</code> value.
However, because doubles are encoded in the IEEE 754 64-bit double-precision binary floating-point format, the exact value can be unexpected.
<p>For example, <code>new BigDecimal(0.1)</code> yields a <code>BigDecimal</code> object. Its value is
<small><code>0.1000000000000000055511151231257827021181583404541015625</code></small>
which is the nearest number to 0.1 representable as a double.
To get <code>BigDecimal</code> that stores the same value as written in the source code,
use either <code>new BigDecimal("0.1")</code> or <code>BigDecimal.valueOf(0.1)</code>.</p>
<p><b>Example:</b></p>
<pre><code>
class Constructor {
void foo() {
new BigDecimal(0.1);
}
}
</code></pre>
<p>After the quick-fix is applied:</p>
<pre><code>
class Constructor {
void foo() {
new BigDecimal("0.1");
}
}
</code></pre>
<!-- tooltip end -->
</body>
</html>