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

NumberFormat.format() HALF_UP rounding is incorrect: 16.665 -> 16.66

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P3
    • Resolution: Not an Issue
    • Affects Version/s: 13
    • Fix Version/s: None
    • Component/s: core-libs
    • Labels:
      None

      Description

      (submitting this on behalf of Roman Ushakov)

      See this test:

      import java.math.RoundingMode;
      import java.text.DecimalFormat;
      import java.text.DecimalFormatSymbols;
      import java.text.NumberFormat;
      import java.util.Locale;
       
      class HalfUpBug {
          public static DecimalFormat createDecimalFormat(int accuracy) {
              DecimalFormat format = (DecimalFormat) NumberFormat.getInstance(Locale.getDefault());
              DecimalFormatSymbols symbols = format.getDecimalFormatSymbols();
              symbols.setGroupingSeparator(' ');
              symbols.setDecimalSeparator('.');
              format.setDecimalFormatSymbols(symbols);
              format.setMinimumFractionDigits(accuracy);
              format.setMaximumFractionDigits(accuracy);
              return format;
          }
       
          public static void main(String[] args) {
              final DecimalFormat decimalFormat = createDecimalFormat(2);
              System.out.println(decimalFormat.format(16.665));
              decimalFormat.setRoundingMode(RoundingMode.HALF_UP);
              System.out.println(decimalFormat.format(16.665));
          }
      }

      On current jdk/jdk it produces:

      16.66
      16.66

      ...while users expect it to be 16.67.

      It looks similar to already fixed JDK-8039915.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                naoto Naoto Sato
                Reporter:
                shade Aleksey Shipilev
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: