JetBrains/intellij-community · error · FormatDecode.IllegalFormatException

invalid precision specified in ''{0}''

Error message

invalid precision specified in ''{0}''

What it means

IllegalFormatException from FormatDecode when a '.' precision marker is present but has no digits — precision != null && precision.length() < 2 means the captured group is just '.' (the group includes the dot, so valid precisions are '.N' with length >= 2). E.g. '%.f' or '%10.s' is invalid; java.util.Formatter requires at least one digit after '.'.

Source

Thrown at java/java-analysis-impl/src/com/siyeh/ig/format/FormatDecode.java:359

          case 'a', 'A' -> { // hexadecimal floating-point number
            checkFlags(flagBits, ~(PARENTHESES | GROUP), specifier);
            allowed = new FloatValidator(specifier);
          }
          case 'e', 'E' -> { // floating point -> decimal number in computerized scientific notation
            checkFlags(flagBits, ~GROUP, specifier);
            allowed = new FloatValidator(specifier);
          }
          case 'g', 'G' -> { // scientific notation
            checkFlags(flagBits, ~ALTERNATE, specifier);
            allowed = new FloatValidator(specifier);
          }
          case 'f' -> // floating point -> decimal number
            allowed = new FloatValidator(specifier);
          default -> throw new IllegalFormatException(InspectionGadgetsBundle.message("format.string.error.unknown.conversion", specifier));
        }
      }
      if (precision != null && precision.length() < 2) {
        throw new IllegalFormatException(InspectionGadgetsBundle.message("format.string.error.invalid.precision", specifier));
      }
      if (isAllBitsSet(flagBits, LEADING_SPACE | PLUS)) {
        throw new IllegalFormatException(
          InspectionGadgetsBundle.message("format.string.error.illegal.flag.combination", ' ', '+', specifier));
      }
      if (isAllBitsSet(flagBits, LEFT_JUSTIFY | ZERO_PAD)) {
        throw new IllegalFormatException(
          InspectionGadgetsBundle.message("format.string.error.illegal.flag.combination", '-', '0', specifier));
      }
      if (StringUtil.isEmpty(width)) {
        if (isAllBitsSet(flagBits, LEFT_JUSTIFY)) {
          throw new IllegalFormatException(InspectionGadgetsBundle.message("format.string.error.left.justify.no.width", specifier));
        }
        if (isAllBitsSet(flagBits, ZERO_PAD)) {
          throw new IllegalFormatException(InspectionGadgetsBundle.message("format.string.error.zero.padding.no.width", specifier));
        }
      }
      storeValidator(allowed, pos, parameters, argumentCount);

View on GitHub (pinned to be881553f2)

Solutions

  1. Add digits after the precision dot ('%.2f') or remove the dot entirely ('%f')
  2. When generating formats from variables, omit '.' when the precision value is empty

Example fix

// before
String.format("%.f", 3.14159)
// after
String.format("%.2f", 3.14159)
Defensive patterns

Strategy: validation

Validate before calling

static boolean precisionWellFormed(String spec) {
  int dot = spec.indexOf('.');
  if (dot < 0) return true;
  return dot + 1 < spec.length() && Character.isDigit(spec.charAt(dot + 1));
}

Try / catch

try { FormatDecode.decode(fmt, argc); } catch (IllegalFormatException e) { /* add a digit after '.' or drop the '.' */ }

Prevention

When it happens

Trigger: A specifier like '%.f' or '%5.3' missing digits after the dot in the general case (no conversion-level exemption applied yet); decode() checks this after resolving the validator.

Common situations: Typos deleting the precision digit; template placeholders emitting '.' with an empty precision variable; localizing formats where the digit was dropped in translation.

Related errors


AI-assisted analysis of JetBrains/intellij-community@be881553f2 (2026-08-14). Data as JSON: /api/errors/2f8324c8f0424c46. Report an issue: GitHub.