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

DecimalFormat.setGroupingSize(int) allows setting negative grouping size

    Details

    • Type: Bug
    • Status: Open
    • Priority: P4
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: core-libs
    • Labels:
      None

      Description

      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

        Attachments

          Activity

            People

            • Assignee:
              naoto Naoto Sato
              Reporter:
              nishjain Nishit Jain (Inactive)
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated: