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

Unpredictable behavior of DecimalFormat.parse when parseIntegerOnly is set to true

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P4
    • Resolution: Won't Fix
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: core-libs
    • Labels:
      None

      Description

      The behavior of "parse integer only" is not well described in DecimalFormat, specially with the instances having multiplier != 1. For example, getPrecentInstance()


      - With the general number format instance, it skips fractional part which gives the expected output and is well documented in specification

              DecimalFormat format = (DecimalFormat)NumberFormat.getNumberInstance();
              format.setParseIntegerOnly(true);
              System.out.println(""+format.parse("123.45")); // Output: 123

      Outputs 123 (skipping fractional part)

      But, with instances where multiplier is involved e.g. percent instance
      1. In some cases it throws exception

           DecimalFormat format = (DecimalFormat)NumberFormat.getPercentInstance();
           format.setParseIntegerOnly(true);
           Number num = format.parse("20.99%"); // THROWS EXCEPTION

      Output: THROWS EXCEPTION

      2. With percent instance and no fraction digits, it returned 0, when expected value may be 0.20

           DecimalFormat format = (DecimalFormat)NumberFormat.getPercentInstance();
           format.setParseIntegerOnly(true);
           Number num = format.parse("20%");

      Output: 0

      The overall the behavior of DecimalFormat.parse() is very unpredictable when "parse integer only" is true.

        Attachments

          Activity

            People

            • Assignee:
              nishjain Nishit Jain
              Reporter:
              nishjain Nishit Jain
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: