apache/druid · error · NumberFormatException
Cannot parse string to double
Error message
Cannot parse string to double
What it means
Numbers.parseDoubleObject(String) parses a String to a boxed Double using Guava's Doubles.tryParse. If the string cannot be parsed as a double, it throws NumberFormatException "Cannot parse string to double". It is the strict (exploding) counterpart of tryParseDouble.
Solutions
- Validate/trim and normalize the string (strip units, commas, whitespace) before parsing.
- Use the lenient Numbers.tryParseDouble(s, nullValue) or Doubles.tryParse(s) and handle null instead of throwing.
- Catch NumberFormatException and treat the value as null/NaN.
- Fix the data source so the field is genuinely numeric.
Example fix
// before
Double d = Numbers.parseDoubleObject(str);
// after
Double d = Doubles.tryParse(str == null ? null : str.trim());
if (d == null) { d = 0.0; /* or skip row */ } Defensive patterns
Strategy: try-catch
Validate before calling
Double d = (str == null) ? null : Doubles.tryParse(str.trim());
if (d == null) { throw new IllegalArgumentException("Not a double: " + str); } Type guard
boolean isDoubleString(String s) { return s != null && Doubles.tryParse(s.trim()) != null; } Try / catch
Double d;
try { d = Numbers.parseDoubleObject(s); } catch (NumberFormatException e) { LOG.warn("Unparseable double: %s", s); d = null; } Prevention
- Prefer lenient Doubles.tryParse/tryParseDouble when nulls are acceptable
- Trim and normalize locale-specific formatting before parsing
- Validate string fields are numeric at ingestion time
When it happens
Trigger: Calling Numbers.parseDoubleObject(s) with a non-numeric or malformed string such as "abc", "1,000" (comma groupings), an empty string, or a string with currency symbols.
Common situations: Parsing CSV/config/JSON string fields that are expected numeric but contain blanks, locale-formatted numbers, or stray whitespace/units (e.g. "10ms").
Understand the failure class
Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.
Related errors
- Cannot parse string to long
- Failed to parse array element
- Invalid format or out of range of long
- Key[ ] should be a long, was[ ]
- Key[ ] should be an int, was[ ]
AI-assisted analysis of apache/druid@9b90983fd2 (2026-09-07).
Data as JSON: /api/errors/b220f63ccf5d2f7a.
Report an issue: GitHub.
Appendix: source
Thrown at processing/src/main/java/org/apache/druid/java/util/common/Numbers.java:185
throw new IAE("Unknown object type [%s]", val.getClass().getName());
}
}
/**
* Like {@link #tryParseDouble}, but does not produce a primitive and will explode if unable to produce a Double
* similar to {@link Double#parseDouble}
*/
@Nullable
public static Double parseDoubleObject(@Nullable String val)
{
if (val == null) {
return null;
}
Double d = Doubles.tryParse(val);
if (d != null) {
return d;
}
throw new NumberFormatException("Cannot parse string to double");
}
/**
* Like {@link #tryParseLong} but does not produce a primitive and will explode if unable to produce a Long
* similar to {@link Long#parseLong}
*/
@Nullable
public static Long parseLongObject(@Nullable String val)
{
if (val == null) {
return null;
}
Long lobj = Longs.tryParse(val);
if (lobj != null) {
return lobj;
}
// try as a double, for "ddd.dd" , Longs.tryParse(..) returns null
Double dobj = Doubles.tryParse(val);View on GitHub (pinned to 9b90983fd2)