Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8358804

Improve the API Note of BigDecimal.valueOf(double)

XMLWordPrintable

    • b03

      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

            rgiulietti Raffaello Giulietti
            rgiulietti Raffaello Giulietti
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: