How do I convert a String to a double with Java?

Question: How do I convert a String to a double with Java?

Answer: Converting a String to a double requires that you first convert the String to a Double object, then convert the Double object to a double data type (Double is an object, while double is a primitive data type).

Here’s the source code for an example Java program that performs this conversion:

public class ConvertStringToDouble {

   public static void main (String[] args) {

      // String s = "fred";    // do this if you want an exception

      String s = "100.00";

      try {
         double d = Double.valueOf(s.trim()).doubleValue();
         System.out.println("double d = " + d);
      } catch (NumberFormatException nfe) {
         System.out.println("NumberFormatException: " + nfe.getMessage());

Other notes

  • Double.toString(double d) is used to convert in the other direction, from a double to a String.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.