How to Convert String to Double in Java

In this tutorial, we will learn how to convert string to primitive type double or double wrapper class object in Java easily.

There are certain situations where we need to convert a number represented as a string into a double type in Java.

We normally use it when we want to perform mathematical operations on the string which contains a number.

For example, whenever we gain data from JTextField or JComboBox, we receive entered data as a string. If entered data is a double number in string form, we need to convert the string to a double to perform mathematical operations on it.

There are the following ways to convert a string to a primitive type double in Java. They are as:

  • Convert using Double.parseDouble()
  • Convert using Double.valueOf()
  • Converting using new Double(String).doubleValue()
  • Convert using DecimalFormat

How to convert string to double in Java

Convert String to Double in Java using Double.parseDouble()


To convert string to a double, we use Double.parseDouble() method from the Double class. The parseDouble() of Double class is the static utility method.

So, we do not need to create an object of class to call it. We can call it simply using its class name. The general signature of parseDouble() method is as below:

public static double parseDouble(String s)

This method accepts a string containing the double representation to be parsed. It returns the double numeric value.

The parseDouble() method throws an exception named NumberFormatException if the string does not contain a parsable double.


Let’s take a simple example program to convert a string to a double.

Program code 1:

// Java Program to demonstrate the conversion of String into primitive double 
// using Double.parseDouble() method
package javaConversion;
public class StringToDoubleConversion {
public static void main(String[] args) 
{
// Create a string literal   
   String str = "50699.60";  
// Convert string into double using Double.parseDouble() method. 
   double num = Double.parseDouble(str);  
// Print value of num.  
   System.out.println(num + 50);
   System.out.println(str + 50);
 }
}
Output:
      50749.6
      50699.6050

Java String to Double Conversion using Double.valueOf()


To convert a string to a double numeric value, we normally use Double.valueOf() method of Java Double class. The general signature of this method is as below:

public static Double valueOf​(String str)

This method converts a string str containing a double number into a Double class object and returns that object holding the value of the specified string.

The valueOf() method throws an exception named NumberFormatException if the string cannot be parsed as a double.


Let’s take a very simple example program to convert String to double class object in Java.

Program code 2:

// Java Program to demonstrate the conversion of String into Double class object.  
// using Double.valueOf() method
package javaConversion;
public class StringToDoubleConversion {
public static void main(String[] args) 
{
// Create a string literal   
   String str = "50699.60";  
// Convert string into double class object using Double.valueOf() method. 
   Double num = Double.valueOf(str);  
// Print value of num.  
   System.out.println(num + 50);
   System.out.println(str + 50);
 }
}
Output:
      50749.6
      50699.6050

Converting String into Double using new Double(String).doubleValue()


Another alternative approach is to create an instance of Double class and then call doubleValue() method of Double class.

The doubleValue() method converts the double object into primitive double type numeric value. This is called “unboxing” in Java.

Let’s create a Java program to convert String into a double object using doubleValue() method.

Program code 3:

// Java Program to demonstrate the conversion of String into double class object.  
// using Double(String).doubleValue() method
package javaConversion;
public class StringToDoubleConversion {
public static void main(String[] args) 
{
// Create a string literal   
   String str = "50699.60";  
// Convert string into double class object using Double(String).doubleValue() method. 
   Double num = new Double(str);
   double x = num.doubleValue();  
// Print value of num.  
   System.out.println(num + 50);
 }
}
Output:
      50749.6

Converting String to Double using DecimalFormat


Java provides a class called DecimalFormat that allows to convert a number to its string representation. This class is present in java.text package. We can also use in other way to parse a string into its numerical representation.

Let’s make a Java program for the conversion of a string into double type numeric value using DecimalFormat class.

Program code 4:

// Java Program to demonstrate the conversion of String into double object using DecimalFormat class.
package javaConversion;
import java.text.DecimalFormat;
import java.text.ParseException;
public class StringToDoubleConversion {
public static void main(String[] args) 
{
// Create a string literal   
   String str = "50699.60";  
// Create an object of DecimalFormat class.    
   DecimalFormat decimalFormat = new DecimalFormat("#");
   try {
     double num = decimalFormat.parse(str).doubleValue();
     System.out.println(num + 80);
     System.out.println(str + 80);
   } catch (ParseException e) {
     System.out.println(str + " is not a valid number.");
   }
 }
}
Output:
      50779.6
      50699.6080

NumberFormatException Case


If we don’t have a number in string literal, invoking Double.parseDouble() or Double.valueOf() methods can throw an exception named NumberFormatException. Let’s take an example program based on it.

Program code 5:

// Java Program to demonstrate the case of NumberFormatException exception.
package javaConversion;
public class StringToDoubleConversion {
public static void main(String[] args) 
{
// Create a string literal   
   String str = "Hello";  
// Call parseDouble() method of Double class.     
   Double num = Double.parseDouble(str);  
   System.out.println(num);
 }
}
Output:
      Exception in thread "main" java.lang.NumberFormatException: For input string: "Hello"
	at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2054)
	at java.base/jdk.internal.math.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
	at java.base/java.lang.Double.parseDouble(Double.java:651)
	at javaConversion.StringToDoubleConversion.main(StringToDoubleConversion.java:9)

In this tutorial, you learned how to convert a string to primitive double type numeric value in different ways. Hope that you will have understood all the methods to convert string to double and practiced all programs.
Thanks for reading!!!
Next ⇒ Convert Double to String in Java⇐ Prev Next ⇒

Please share your love