{"record":{"id":"418fba400b7c8c55","repo":"bazelbuild/bazel","slug":"got-s-for-c-format-want-int-or-float","errorCode":null,"errorMessage":"got %s for '%%%c' format, want int or float","messagePattern":"got (.+?) for '%%%c' format, want int or float","errorType":"exception","errorClass":"MissingFormatWidthException","httpStatus":null,"severity":"error","filePath":"src/main/java/net/starlark/java/eval/Printer.java","lineNumber":437,"sourceCode":"      }\n      Object arg = arguments.get(a++);\n\n      switch (conv) {\n        case 'd', 'o', 'x', 'X' -> {\n          Number n =\n              switch (arg) {\n                case StarlarkInt starlarkInt -> starlarkInt.toNumber();\n                case Integer integer -> integer;\n                case StarlarkFloat starlarkFloat -> {\n                  double d = starlarkFloat.toDouble();\n                  try {\n                    yield StarlarkInt.ofFiniteDouble(d).toNumber();\n                  } catch (IllegalArgumentException unused) {\n                    throw new MissingFormatWidthException(\"got \" + arg + \", want a finite number\");\n                  }\n                }\n                default ->\n                    throw new MissingFormatWidthException(\n                        String.format(\n                            \"got %s for '%%%c' format, want int or float\",\n                            Starlark.type(arg), conv));\n              };\n          printer.append(\n              String.format(\n                  conv == 'd' ? \"%d\" : conv == 'o' ? \"%o\" : conv == 'x' ? \"%x\" : \"%X\", n));\n        }\n\n        case 'e', 'f', 'g', 'E', 'F', 'G' -> {\n          double v =\n              switch (arg) {\n                case Integer integer -> (double) integer;\n                case StarlarkInt starlarkInt -> starlarkInt.toDouble();\n                case StarlarkFloat starlarkFloat -> starlarkFloat.toDouble();\n                default ->\n                    throw new MissingFormatWidthException(\n                        String.format(","sourceCodeStart":419,"sourceCodeEnd":455,"githubUrl":"https://github.com/bazelbuild/bazel/blob/e6e199d0601a244511b4cf18c8b2828aa73db1fd/src/main/java/net/starlark/java/eval/Printer.java#L419-L455","documentation":"Thrown by %d/%o/%x/%X formatting when the argument is neither int nor float — the switch over the argument's runtime type falls through to the default branch. The message interpolates the actual Starlark type name and the conversion character, e.g. got string for '%d' format, want int or float.","triggerScenarios":"\"%d\" % \"12\", \"%x\" % [255], \"%d\" % None, \"%o\" % depset([1]) — any non-numeric argument to an integer conversion.","commonSituations":"Values read from labels, environment strings, or structured data are strings and are formatted as if numeric; None defaults flowing into a count field; forgetting that %-formatting does not coerce types.","solutions":["Convert explicitly before formatting: \"%d\" % int(s) (guard the conversion if the string may be non-numeric).","Use %s if you just want the value rendered.","Default optional values to 0 rather than None when they feed %d."],"exampleFix":"# before\n\"retry %d\" % attempt  # attempt is \"3\"\n\n# after\n\"retry %d\" % int(attempt)","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"def is_number(v):\n    return type(v) in (\"int\", \"float\")\n\nassert is_number(v), \"%%d needs int/float, got %s\" % type(v)","tryCatchPattern":null,"preventionTips":["Convert strings with int()/float() before numeric formatting.","Default optional numeric fields to 0, not None."],"tags":["starlark","format","percent-format","type-mismatch"],"backgroundTag":null,"analyzedSha":"e6e199d0601a244511b4cf18c8b2828aa73db1fd","analyzedAt":"2026-08-14T10:24:27.848Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}