{"record":{"id":"5802195973c323f6","repo":"Tencent/APIJSON","slug":"cannot-convert-string-value-value-to-dou","errorCode":null,"errorMessage":"Cannot convert String value '\" + value + \"' to double: \" + e.getMessage()","messagePattern":"Cannot convert String value '\" \\+ value \\+ \"' to double: \" \\+ e\\.getMessage\\(\\)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"APIJSONORM/src/main/java/apijson/JSON.java","lineNumber":497,"sourceCode":"\t * @param key The key\n\t * @return The double value\n\t * @throws IllegalArgumentException If value cannot be converted to double\n\t */\n\tpublic static Float getFloat(Map<String, Object> map, String key) throws IllegalArgumentException {\n\t\tObject value = map == null || key == null ? null : map.get(key);\n\t\tif (value == null) {\n\t\t\treturn null;\n\t\t}\n\n\t\tif (value instanceof Number) {\n\t\t\treturn ((Number) value).floatValue();\n\t\t}\n\n\t\tif (value instanceof String) {\n\t\t\ttry {\n\t\t\t\treturn Float.parseFloat((String) value);\n\t\t\t} catch (NumberFormatException e) {\n\t\t\t\tthrow new IllegalArgumentException(\"Cannot convert String value '\" + value + \"' to double: \" + e.getMessage());\n\t\t\t}\n\t\t}\n\n\t\tthrow new IllegalArgumentException(\"Cannot convert value of type \" + value.getClass().getName() + \" to double\");\n\t}\n\n\t/**\n\t * Get a double value from a Map\n\t * @param map Source map\n\t * @param key The key\n\t * @return The double value\n\t * @throws IllegalArgumentException If value cannot be converted to double\n\t */\n\tpublic static float getFloatValue(Map<String, Object> map, String key) throws IllegalArgumentException {\n\t\tObject value = map == null || key == null ? null : map.get(key);\n\t\tif (value == null) {\n\t\t\treturn 0;\n\t\t}","sourceCodeStart":479,"sourceCodeEnd":515,"githubUrl":"https://github.com/Tencent/APIJSON/blob/5284052872898eddc449a58f629e5c8d588b8e22/APIJSONORM/src/main/java/apijson/JSON.java#L479-L515","documentation":"Thrown by apijson.JSON.getFloat(Map, String) (APIJSONORM/src/main/java/apijson/JSON.java:497) when the value is a String that Float.parseFloat cannot parse. The message text is misleading: it says \"to double\" but the method parses a float — a copy-paste artifact in the source; the real failure is a non-numeric string (empty, alphabetic, malformed decimal, stray locale comma).","triggerScenarios":"Value is \"12,5\" (European decimal comma), \"\", \"NaN-\", \"abc\", \" 1.5 \" with whitespace, or null-spelled \"null\"; a currency string like \"$1.50\" or \"1.50 USD\".","commonSituations":"Locale-formatted prices and rates serialized as strings; blank strings for optional numeric fields; currency/unit text embedded in the value; developers misdirected by the \"double\" wording into debugging the wrong method.","solutions":["Ignore the \"double\" wording — check the string at this key: trim it and normalize the decimal separator (',' → '.').","Strip non-numeric decoration (currency symbols, units) before parsing, or fix the producer to send a bare JSON number.","Treat blank/null-text values as absent (getFloat returns null for null) rather than letting parseFloat fail.","For double precision needs, use getDouble/getDoubleValue instead of getFloat."],"exampleFix":"// before\nFloat price = JSON.getFloat(item, \"price\"); // throws on \"12,5\"\n\n// after\nObject raw = item.get(\"price\");\nFloat price = raw instanceof Number ? ((Number) raw).floatValue()\n        : (raw instanceof String && !((String) raw).isBlank() ? Float.parseFloat(((String) raw).trim().replace(',', '.')) : null);","handlingStrategy":"validation","validationCode":"Object v = item.get(\"price\");\nif (v instanceof String && !((String) v).trim().matches(\"[+-]?(\\\\d+(\\\\.\\\\d*)?|\\\\.\\\\d+)([eE][+-]?\\\\d+)?\")) {\n    throw new IllegalArgumentException(\"'price' not numeric text: \" + v);\n}","typeGuard":"static boolean isParsableFloat(Object v) {\n    if (v instanceof Number) return true;\n    if (v instanceof String) return ((String) v).trim().matches(\"[+-]?(\\\\d+(\\\\.\\\\d*)?|\\\\.\\\\d+)([eE][+-]?\\\\d+)?\");\n    return false;\n}","tryCatchPattern":"try {\n    Float price = JSON.getFloat(item, \"price\");\n} catch (IllegalArgumentException e) {\n    log.warn(\"Bad float at 'price' (message says 'double' — known wording bug): {}\", e.getMessage());\n    // normalize locale separators or reject\n}","preventionTips":["Remember getFloat's messages say 'double' — the wording is wrong, the failure is float parsing.","Normalize locale decimal commas and strip currency decoration before parsing.","Prefer bare JSON numbers for numeric fields."],"tags":["json","type-conversion","apijson","number-parsing","error-message"],"backgroundTag":null,"analyzedSha":"5284052872898eddc449a58f629e5c8d588b8e22","analyzedAt":"2026-08-14T15:15:29.577Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}