Undocumented exception from String.format() when using "%#g"

XMLWordPrintable

    • Type: Bug
    • Resolution: Unresolved
    • Priority: P5
    • None
    • Affects Version/s: 25
    • Component/s: core-libs
    • None
    • Environment:

      openjdk version "25" 2025-09-16
      OpenJDK Runtime Environment (build 25+36-3489)
      OpenJDK 64-Bit Server VM (build 25+36-3489, mixed mode, sharing)

    • generic
    • generic

      In the Javadoc for java.util.Formatter, in the "Flags" section, the table shows "y" (meaning "the flag is supported for the indicated argument types") for flag '#' and "Floating Point" types.

      However, there are actually some combinations that are not supported. In particular, these format strings generate an exception "%#g" and "%#G".

      The table entry should have a footnote that notes these two exceptions - or if the exception is unintended, it should be fixed (I'm not sure which is the case).

      Reproducer:

      $ jshell
      | Welcome to JShell -- Version 25
      | For an introduction type: /help intro

      jshell> String.format("%#e", 3.35);
      $1 ==> "3.350000e+00"

      jshell> String.format("%#E", 3.35);
      $2 ==> "3.350000E+00"

      jshell> String.format("%#f", 3.35);
      $3 ==> "3.350000"

      jshell> String.format("%#g", 3.35);
      | Exception java.util.FormatFlagsConversionMismatchException: Conversion = g, Flags = #
      | at Formatter$FormatSpecifier.failMismatch (Formatter.java:4658)
      | at Formatter$FormatSpecifier.checkBadFlags (Formatter.java:3410)
      | at Formatter$FormatSpecifier.checkFloat (Formatter.java:3422)
      | at Formatter$FormatSpecifier.check (Formatter.java:3139)
      | at Formatter$FormatSpecifier.<init> (Formatter.java:3126)
      | at Formatter$FormatSpecifierParser.parse (Formatter.java:2915)
      | at Formatter.parse (Formatter.java:2814)
      | at Formatter.format (Formatter.java:2744)
      | at Formatter.format (Formatter.java:2698)
      | at String.format (String.java:4455)
      | at (#1:1)

      jshell> String.format("%#G", 3.35);
      | Exception java.util.FormatFlagsConversionMismatchException: Conversion = g, Flags = #
      | at Formatter$FormatSpecifier.failMismatch (Formatter.java:4658)
      | at Formatter$FormatSpecifier.checkBadFlags (Formatter.java:3410)
      | at Formatter$FormatSpecifier.checkFloat (Formatter.java:3422)
      | at Formatter$FormatSpecifier.check (Formatter.java:3139)
      | at Formatter$FormatSpecifier.<init> (Formatter.java:3126)
      | at Formatter$FormatSpecifierParser.parse (Formatter.java:2915)
      | at Formatter.parse (Formatter.java:2814)
      | at Formatter.format (Formatter.java:2744)
      | at Formatter.format (Formatter.java:2698)
      | at String.format (String.java:4455)
      | at (#2:1)

            Assignee:
            Unassigned
            Reporter:
            Archie Cobbs
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: