The API Note of `BigDecimal.valueOf(double)` is about the preferred way to convert a floating-point value to a `BigDecimal`.
However, for a `float` value it usually returns a `BigDecimal` that is too precise, that is, with unnecessarily too many digits.
This is because there's no more specific overloading that accepts a `float`, so the input is first converted to a `double` at the call sites, leading to more digits than strictly required for `float`.
The note should, instead, suggest to convert a `float val` by executing `new BigDecimal(Float.toString(val))`.
Adding a `float` overload could behaviorally break clients that invoke this method on `float`s, when they are recompiled.
Discussion here:
https://mail.openjdk.org/pipermail/core-libs-dev/2025-January/139042.html
However, for a `float` value it usually returns a `BigDecimal` that is too precise, that is, with unnecessarily too many digits.
This is because there's no more specific overloading that accepts a `float`, so the input is first converted to a `double` at the call sites, leading to more digits than strictly required for `float`.
The note should, instead, suggest to convert a `float val` by executing `new BigDecimal(Float.toString(val))`.
Adding a `float` overload could behaviorally break clients that invoke this method on `float`s, when they are recompiled.
Discussion here:
https://mail.openjdk.org/pipermail/core-libs-dev/2025-January/139042.html
- links to
-
Commit(master) openjdk/jdk/26848a7d
-
Review(master) openjdk/jdk/25805