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

DecimalFormat.setGroupingSize(int) allows setting negative grouping size

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 14
    • None
    • core-libs
    • b19
    • Verified

      The DecimalFormat.setGroupingSize(int) allows setting negative values, which may not be correct for a property like grouping size.

      DecimalFormat fmt = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US);
      fmt.setGroupingSize(-2);
      System.out.println(fmt.getGroupingSize());

      Instead of throwing exception, the above code snippet accepts -2 to be set for grouping size

      Expected: Throw an IllegalArgumentException
      Actual: Negative values are accepted.

      Also, the specification of setGroupingSize() says that "The value passed in is converted to a byte, which may lose information."

      so, setting a value outside "byte" boundary, wraps around e.g.
      fmt.setGroupingSize(255); // makes the grouping size as -1

      Instead of accepting all integer values, a range check should be applied before setting the grouping size field. e.g. values between 0-127

            naoto Naoto Sato
            nishjain Nishit Jain
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: