5 ways : How to Convert from String to double in Java

There are many ways by which we can convert from String to double in java. The most common methods available are Double.parseDouble(String) and Double.valueOf(String) methods (both methods belong to Double class). In this tutorial I will be sharing 4 ways to convert String to double in java.

Read Also:  How to convert String to int in java without using parseInt() method

1. Using Double.parseDouble(String) method [Preferred]
2. Using Double.valueOf(String) method
3. Using DecimalFormat
4. Using Double class constructor [Deprecated in java 9]
5. Convert String to number without Exponential

1. Using Double.parseDouble(String) method

The syntax for the static method Double.parseDouble(String) is :

public static double parseDouble(String str) throws NumberFormatException
The parameter str of type String will be converted to the double primitive data type. You can see that parseDouble(String) method throws NumberFormatException if the String format is not valid.
For example if the String is "99.99xyz" then parseDouble(String) method will throw NumberFormatException.

Note: Double numbers are also represented by appending "d" or "D" to the last digit of the number.
Below cases to convert String to double are valid.

Valid cases :

        String str = "99.99d"; 
        double doubleNum = Double.parseDouble(str);
        System.out.println("Convert String to double " + doubleNum);

Output :
Convert String to double 99.99

        String str = "999.99D"; 
        double doubleNum = Double.parseDouble(str);
        System.out.println("Convert String to double " + doubleNum);

Output :
Convert String to double 999.99

Example :

This method is the most popular method to convert String to double primitive data type.

public class StringToDouble {
    public static void main(String args[]) {  
        String str = "1011.99";
        double doubleNum = Double.parseDouble(str);
        System.out.println("Convert String to double " + doubleNum);
    }
}

Output :
Convert String to double 1011.99

2. Using Double.valueOf(String) method

The syntax for the static method Double.valueOf(String) is:

public static Double valueOf(String str) throws NumberFormatException

The parameter str of type String will be converted to the Double object holding the value of str String. Just like Double.parseDouble(String) method this method also throws NumberFormatException if the String str format is not valid.

Double.valueOf(String) method is the most popular method to convert String to Double class object.

Note: The resulting value is an object of the Double class not the double primitive value.

Example :


public class StringToDouble {
    public static void main(String args[]) {
        String str = "1011.99";
        Double doubleNum = Double.valueOf(str);
        System.out.println("Convert String to double " + doubleNum);
    }
}

Output :
Convert String to double 1011.99

3. Using DecimalFormat

The class java.text.DecimalFormat parse(String str) method can be used to convert String to double.
parse(String str) method will throw ParseException if the String str format is not a valid .

import java.text.*;
public class StringToDouble {
    public static void main(String args[]) {
        String str = "1001.25"; 
        DecimalFormat df = new DecimalFormat("#");    
        try {
            double doubleNum = df.parse(str).doubleValue();
            System.out.println(" Convert String to double "+ doubleNum);
        }    
        catch (ParseException e) {
            System.out.println(" Invalid String Format "+ str);
        }
    }
}

Output :
Convert String to double 1001.25

4. Using Double Class Constructor [Deprecated]

Another way to convert String to double is using Double class constructor. You need to further invoke the doubleValue() method on Double object to get the resulting value in double primitive.

public class StringToDouble {
    public static void main(String args[]) {
        String str = "1001.25"; 
        Double doubleObj = new Double(str);
        double doubleNum = doubleObj.doubleValue();
        System.out.println("Convert String to double "+ doubleNum);
    }
}

Output :
Convert String to double 1001.25

5. Convert String to number without Exponential

The problem with all of the above 4 methods is for large String numbers, the resulting double value prints in exponential format. For example

        String str = "111111111111.09d"; 
        double doubleNum = Double.parseDouble(str);
        System.out.println("Convert String to double " + doubleNum);

Output :
Convert String to double 1.1111111111109E11

As you can see the Output, the resulting double value is exponential.

If you want an output which is a number not a String and without exponential, then you need to convert the String value to the BigDecimal.

import java.math.*;
public class StringToDouble {
    public static void main(String args[]) {
        String str = "111111111111.123456789"; 
        double doubleNum = Double.parseDouble(str);
        System.out.println("Print Exponential Format : " + doubleNum);
        System.out.println("Print in BigDecimal Format : " + 
                                           new BigDecimal(str));
    }
}

Output :
Print Exponential Format : 1.1111111111112346E11
Print in BigDecimal Format : 111111111111.123456789


Please mention in the comments if you have any questions regarding conversion of String to double in java.

References :
Double Oracle doc
Double valueOf() doc

About The Author

Subham Mittal has worked in Oracle for 3 years .
For more java articles ,Click here to Subscribe JavaHungry