Java – Convert String to Double

You can typecast or convert a String to Double in Java in many ways. Some of them are using Double.parseDouble(), Double.valueOf(), new Double().

In this tutorial, we shall learn some of the ways of how to convert a string value to a double value with examples.

1. Convert string to double using Double.parseDouble()

Double.parseDouble(str) parses any parsable double value from string to double value.

In this example, we shall use Double.parseDouble() method and pass a string that can be parsed to a valid double value.

Java Program

</>
Copy
/**
 * Java Program - Convert String to Double
 */

public class StringToDouble {

	public static void main(String[] args) {
		//a string
		String str = "7.89654263548";
		
		//convert string to double
		double d = Double.parseDouble(str);
		
		System.out.print(d);
	}
}

Run the above program and the String is converted to Double.

7.896542635488555

If you do not provide a valid string that is parsable double, Double.parseDouble() throws NumberFormatException.

In the following example program, we shall take a string which does not contain a valid double value.

Some of the scenarios that could throw this error are:

  • If the string contains invalid characters that does not parse to a double value. Like decimal point, alphabets, etc.
  • A number that is out of range for a double value. Any value that is outside the range [-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807] will make parseDouble() to throw this error.

Java Program

</>
Copy
/**
 * Java Program - Convert String to Double
 */

public class StringToDouble {

	public static void main(String[] args) {
		//a string
		String str = "5-354";
		
		//convert string to double
		double n = Double.parseDouble(str);
		
		System.out.print(n);
	}
}

Run the above program and parseDouble() throws NumberFormatException.

Exception in thread "main" java.lang.NumberFormatException: For input string: "5-354"
	at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(Unknown Source)
	at java.base/jdk.internal.math.FloatingDecimal.parseDouble(Unknown Source)
	at java.base/java.lang.Double.parseDouble(Unknown Source)
	at StringToDouble.main(StringToDouble.java:12)

Also, if null is passed to Double.parseDouble(), the function throws NullPointerException.

Java Program

</>
Copy
/**
 * Java Program - Convert String to Double
 */

public class StringToDouble {

	public static void main(String[] args) {
		//a string
		String str = null;
		
		//convert string to double
		double n = Double.parseDouble(str);
		
		System.out.print(n);
	}
}

Run the above program and parseDouble() throws NullPointerException.

Exception in thread "main" java.lang.NullPointerException
	at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(Unknown Source)
	at java.base/jdk.internal.math.FloatingDecimal.parseDouble(Unknown Source)
	at java.base/java.lang.Double.parseDouble(Unknown Source)
	at StringToDouble.main(StringToDouble.java:12)

As Double.parseDouble() can throw NullPointerException or NumberFormatException, it is a good practice to surround the function parseDouble() with Java Try Catch and handle the exceptions accordingly.

</>
Copy
/**
 * Java Program - Convert String to Double
 */

public class StringToDouble {

	public static void main(String[] args) {
		//a string
		String str = "8.561253652147";
		
		double n = 0;

		try {
			//convert string to double
			n = Double.parseDouble(str);
		} catch (NumberFormatException e) {
			System.out.println("Check the string. Not a valid double value.");
		} catch (NullPointerException e) {
			System.out.println("Check the string. String is null.");
		}
		
		System.out.print(n);
	}
}

2. Convert string to double using Double.valueOf()

You can also use Double.valueOf() function to convert a string to double.

In the following example, we shall use the method valueOf() to get double value from string.

Java Program

</>
Copy
/**
 * Java Program - Convert String to Double
 */

public class StringToDouble {

	public static void main(String[] args) {
		String str = "8.561253652147";
		double n = 0;

		try {
			//convert string to double
			n = Double.valueOf(str);
		} catch (NumberFormatException e) {
			System.out.println("Check the string. Not a valid double value.");
		} catch (NullPointerException e) {
			System.out.println("Check the string. String is null.");
		}
		
		System.out.print(n);
	}
}

Just like Double.parseDouble(), the function Double.valueOf() also throws NullPointerException if the string argument is null, or a NumberFormatException if the string does not parse to a valid double value.

3. Convert string to double using Double() constructor

Note: new Double() constructor is depreciated. So, you may get warning when you are following this process. Double.valueOf() is recommended in the place of new Double().

You can also use the constructor of Double class, to convert a string to double.

In the following example, we shall use the constructor of Double class to convert from string to double.

Java Program

</>
Copy
/**
 * Java Program - Convert String to Double
 */

public class StringToDouble {

	public static void main(String[] args) {

		try {
			String value = "-92.26523775807";
			Double d = new Double(value);
			System.out.print(d);
		} catch (NumberFormatException e) {
			System.out.println("Check the string. Not a valid double value.");
		} catch (NullPointerException e) {
			System.out.println("Check the string. String is null.");
		}
		
	}
}

Conclusion

In this Java Tutorial, we learned how to Convert a String to Double value in Java using Double.parseDouble() and Double.valueOf() methods.