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

DecimalFormat roundingMode regression

XMLWordPrintable

    • generic
    • generic

      java.text.DecimalFormat::setRoundingMode does not round input as specified by the rounding mode.
      Regression: Last worked in version 21.0.8.

      Sample :
      import java.math.RoundingMode;
      import java.text.DecimalFormat;

      public class DecimalFormatExample {
          public static void main(String[] args) {
              DecimalFormat format = (DecimalFormat) DecimalFormat.getInstance();
              format.setMaximumFractionDigits(10);
              format.setRoundingMode(RoundingMode.HALF_UP);
              var expected = "0.0000000001";
              var actual = format.format(Double.parseDouble("0.00000000005"));
              if (!expected.equals(actual)) {
                  throw new AssertionError("Expected: " + expected + ", Actual: " + actual);
              }
          }
      }

            jlu Justin Lu
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: