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

DecimalFormat roundUp doesn't work for certain numbers (or maybe because of left over state)

XMLWordPrintable

    • generic
    • generic

      FULL PRODUCT VERSION :
      java version "1.8.0_112"
      Java(TM) SE Runtime Environment (build 1.8.0_112-b15)
      Java HotSpot(TM) 64-Bit Server VM (build 25.112-b15, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 10.0.14931]

      A DESCRIPTION OF THE PROBLEM :
      if you run this simple example:

      public static void main(String[] args) throws ParseException
      {
      DecimalFormat df = new DecimalFormat("#,##0.###", new DecimalFormatSymbols(Locale.getDefault()));
      df.setRoundingMode(RoundingMode.HALF_UP);
      System.out.println(df.format(0.0245));
      System.out.println(df.format(0.0255));
      System.out.println(df.format(0.0265));

      }

      then my output is:

      0.025
      0.025
      0.026

      so the first it does round up but then it doesn't do that anymore.

      If i run this against a java 7 version that i have here it works fine.

      this has something todo with: http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/sun/misc/FloatingDecimal.java#FloatingDecimal.BinaryToASCIIBuffer.dtoa%28int%2Clong%2Cint%2Cboolean%29

      there the roundup method is doing something else then the DecimalFormats roundup stuff and for the above numbers where that goes wrong the boolean decimalDigitsRoundedUp of the class BinaryToASCIIBuffer is true, and then the DecimalFormat just uses that but it didn't really round up correctly.



      REGRESSION. Last worked in version 7u80

      ADDITIONAL REGRESSION INFORMATION:
      I am not sure when the regression started, but with:

      java version "1.7.0_51"
      Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
      Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode)

      it works fine


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      run the above code. Maybe tweak the locale? but don't think that is a leading factor here.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      0.0245 -> 0.025
      0.0255 -> 0.026
      0.0265 -> 0.027
      ACTUAL -
      0.0245 -> 0.025
      0.0255 -> 0.025
      0.0265 -> 0.026

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------

      public static void main(String[] args) throws ParseException
      {
      DecimalFormat df = new DecimalFormat("#,##0.###", new DecimalFormatSymbols(Locale.getDefault()));
      df.setRoundingMode(RoundingMode.HALF_UP);
      System.out.println(df.format(0.0245));
      System.out.println(df.format(0.0255));
      System.out.println(df.format(0.0265));

      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      i didn't find one yet, i guess i need to override the parse method and do a lot myself.

            psonal Pallavi Sonal (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: