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

(fmt) Formatter printf rounding error (double rounding up) for double values close to but below tie

XMLWordPrintable

    • Cause Known
    • generic
    • generic

      PrintStream.printf (i.e. java.util.Formater printf) is suffering the same kind of rounding problems
      that was found in java.text.DecimalFormat/DigitList : erroneous double rounding

      The following code:
        double d = 12.225d;
        System.out.println("BigDecimal output for " + d + " -> " + new BigDecimal(d).toString());
        System.out.printf("test %1$.2f \n", 12.225d);

      shows that printf will wrongly round up, following half-up rounding rule specified by
      Format string syntax in Formatter.

      Result from the above is the following output :
      > BigDecimal output for 12.225 -> 12.2249999999999996447286321199499070644378662109375
      > test 12.23
      the printf call shoud provide "test 12.22" since the double d is below the tie.

            bpb Brian Burkhalter
            olagneau Olivier Lagneau (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: