Convert String to double in Java

Most of the times when dealing with Strings, we might need to Convert String to double in Java. To Convert String to double in Java, the wrapper class Double provides few methods. Double class provides parseDouble() and valueOf() method.

Before checking the conversion with examples, let’s see what is Wrapper class in Java.

What is Wrapper Class in Java ?

A Wrapper class is a class whose object wraps or contains a primitive data type. When we create an object of a wrapper class, it contains a field and in this field, we can store a primitive data type. In other words, we can wrap a primitive value into a wrapper class object.

Let’s see the String to double conversion with an example.

Using Double.parseDouble() method:

Let’s convert String “80.26” to double using parseDouble() method.

Output:

Using Double.valueOf() method:

Output:

Note: Please make sure that all the characters in the String must be digits in order to convert String to double. Otherwise it will throw NumberFormatException.

Let’s see this with an example.

NumberFormatException while converting String to double:

Let’s take String “80.26P” and try to convert it to double.

Output:

Want to know about how to convert String to float in Java, then read tutorial on Convert String to float in Java.