DecimalFormat Rounding Errors for Fractional Ties Near Zero

XMLWordPrintable

    • b20
    • 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);
                }
            }
        }

        This bug happens on tie cases when DecimalFormat rounds a fractional double whose least significant digit is one index past the maximum fraction digits allowed by the format.

              Assignee:
              Justin Lu
              Reporter:
              Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              7 Start watching this issue

                Created:
                Updated:
                Resolved: