DecimalFormat with a percentage in the mask has a rounding error for format of BigDecimal values around zero

XMLWordPrintable

      A DESCRIPTION OF THE PROBLEM :
      A DecimalFormat with a mask containing percentage sign does not respect the rounding mode HALF_UP. This only happens when rounding up from zero to one. See reproduction.

      REGRESSION : Last worked in version 21.0.9

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      String mask = "#%";

      DecimalFormat df = new DecimalFormat();
      df.setParseBigDecimal(true);

      df.setRoundingMode(RoundingMode.HALF_UP);
      df.applyPattern(mask);

      System.out.println(df.format(new BigDecimal("0.005"))); //Outputs 0% (should be 1%)
      System.out.println(df.format(new BigDecimal("0.015"))); //Outputs 2%
      System.out.println(df.format(new BigDecimal("0.025"))); //Outputs 3%

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

      public class DecimalFormatPercentageAroundZero {

      public static void main(String[] args) {
      String mask = "#%";

      DecimalFormat df = new DecimalFormat();
      df.setParseBigDecimal(true);

      df.setRoundingMode(RoundingMode.HALF_UP);
      df.applyPattern(mask);

      System.out.println(df.format(new BigDecimal("0.005"))); //Outputs 0% (should be 1%)
      System.out.println(df.format(new BigDecimal("0.015"))); //Outputs 2%
      System.out.println(df.format(new BigDecimal("0.025"))); //Outputs 3%

      }

      //Java21 ok
      //Java22 and up not ok
      }
      ---------- END SOURCE ----------

      FREQUENCY :
      ALWAYS

            Assignee:
            Anjana Sajeev
            Reporter:
            Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: