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

DecimalFormat does not round up properly for certain numbers with pattern 0.0

    XMLWordPrintable

    Details

    • Subcomponent:
    • CPU:
      generic
    • OS:
      generic

      Description

      ADDITIONAL SYSTEM INFORMATION :
      Tested with jdk1.8.0_241 and jdk11

      A DESCRIPTION OF THE PROBLEM :
      When setting pattern "0.0" into DecimalFormat and formatting double number "40.55" the result is "40.5" but should be "40.6". For other numbers, e.g. "30.55" the result is correct as "30.6".

      Bug is similar to JDK-7131459 which is noted as fixed.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the code provided in the source code sections. Or use this shortcut
      "new DecimalFormat("0.0").format(40.55d)"

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      30.5 --> 30.5
      30.55 --> 30.6
      30.555 --> 30.6
      40.5 --> 40.5
      40.55 --> 40.6 <--- expected
      40.555 --> 40.6

      ACTUAL -
      30.5 --> 30.5
      30.55 --> 30.6
      30.555 --> 30.6
      40.5 --> 40.5
      40.55 --> 40.5 <---- actual
      40.555 --> 40.6


      ---------- BEGIN SOURCE ----------
      import java.math.RoundingMode;
      import java.text.DecimalFormat;

      public final class TestDecimalFormatter {

      public static void main(String[] args) {
      DecimalFormat formatter = new DecimalFormat("0.0");
      formatter.setRoundingMode(RoundingMode.HALF_UP);

      double[] numbers = new double[] { 30.5, 30.55, 30.555, 40.5, 40.55, 40.555 };
      for (double d : numbers) {
      System.out.println("" + d + " --> " + formatter.format(d));
      }
      }
      }

      ---------- END SOURCE ----------

      FREQUENCY : always


        Attachments

          Issue Links

            Activity

              People

              Assignee:
              bpb Brian Burkhalter
              Reporter:
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: