Convert String to double in Java
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The standard way to convert a string to a double in Java is Double.parseDouble(...). It is fast, simple, and correct for normal Java-style numeric text such as "3.14" or "1e6".
The real challenge is not the conversion itself. It is deciding what should happen when the input is invalid, locale-specific, or too precise for binary floating-point.
The Normal Case: Double.parseDouble
This is the basic approach:
If the string is not a valid representation of a double, Java throws NumberFormatException.
So for user input or external data, wrap it:
parseDouble Versus valueOf
Double.valueOf(...) is closely related:
Use parseDouble when you want a primitive double. Use valueOf when you specifically need a Double object.
For most everyday code, parseDouble is the more direct choice.
Handle Whitespace and Validation Intentionally
If input comes from a file or user form, trimming it first is often reasonable:
That said, do not guess too much. If the system requires strict formatting, validate and reject rather than silently normalizing everything.
Locale-Specific Numbers Need a Different Parser
Double.parseDouble expects Java's standard numeric format. It does not understand locale conventions such as "1,23" for one-point-two-three.
For locale-aware parsing, use NumberFormat:
This is the right approach when the input format depends on a user's locale or on a known external format.
Beware of Floating-Point Precision
A double is a binary floating-point type. That means some decimal values cannot be represented exactly.
This is fine for many scientific and general-purpose calculations, but not ideal for exact financial arithmetic. If exact decimal precision matters, parse into BigDecimal instead:
That is not a double, but it is often the correct answer when people think they want one.
Special Values
Java accepts some special text values too:
Those may be legitimate in some systems and dangerous in others. If your input should represent ordinary finite numbers only, validate after parsing:
Common Pitfalls
The biggest pitfall is assuming every numeric-looking string belongs in a double. Locale-specific input, money amounts, and IDs often need a different type or parser.
Another common mistake is swallowing NumberFormatException without explaining what was wrong with the input. Good error messages matter when data comes from users or external systems.
People also forget about precision. Converting to double is easy, but exact decimals may not survive the conversion the way you expect.
Finally, do not use locale-aware parsing accidentally when the format is supposed to be strict and machine-readable. Be explicit about which format the program accepts.
Summary
- Use
Double.parseDouble(...)for normal Java-style numeric strings. - Catch
NumberFormatExceptionwhen input is uncertain. - Use
NumberFormatfor locale-specific formats such as comma decimals. - Use
BigDecimalwhen exact decimal precision matters. - Validate special values such as
NaNandInfinityif your domain does not allow them.

