How to convert String to double in Java?

In this post, we will be looking at different ways of converting a string object into a double value. In some of the methods listed below, we will be converting the string object into the Double wrapper class object and then using the unboxing feature of Java, that Double class object will be converted into a double primitive data type value.

Let’s look at each of the methods one by one

Double.parseDouble() method

The Double class in Java has a static method named parseDouble() which can be used to convert a string object into a double value.

Method declaration – public static double parseDouble(String s)
What does it do? – It will take a string object as an argument which then will be converted to a double value and returned back. In case, if we passed an invalid string object which cannot be converted to a double value, then it will throw a NumberFormatException.

public class Codekru {

	public static void main(String[] args) {
		
		String str = "1323.122";
		double d = Double.parseDouble(str);
		
		System.out.println("double value = " + d);

	}
}

Output –

double value = 1323.122
What if we tried to use this method on an invalid string that cannot be converted to a double value?

In this case, we will get the NumberFormatException as the string is not in a valid format to be converted to a double value.

public class Codekru {

	public static void main(String[] args) {

		String str = "1323.122qwe";
		double d = Double.parseDouble(str);

		System.out.println("double value = " + d);

	}
}

Output –

Exception in thread "main" java.lang.NumberFormatException: For input string: "1323.122qwe"
	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:549)
Now, what if we tried this on a string representing a negative double value?
public class Codekru {

	public static void main(String[] args) {

		String str = "-1323.122";
		double d = Double.parseDouble(str);

		System.out.println("double value = " + d);

	}
}

Output –

double value = -1323.122

Double.valueOf() method

The Double wrapper class has one more static method that can convert a string object into a double value.

Method declaration – public static Double valueOf(String s)
What does it do? It will take the string object as an argument and convert it into a Double class object. In case, it encounters a problem while doing so, ( probably because of an invalid string ) then it will throw a NumberFormatException.

valueOf() method returns a Double class object but because of the unboxing feature of Java, the Double class object value was automatically converted into the double primitive type and is assigned to the variable.

public class Codekru {

	public static void main(String[] args) {

		String str = "1323.122d";
		double d = Double.valueOf(str);

		System.out.println("double value = " + d);

	}
}

Output –

double value = 1323.122

Using Double() constructor

We can convert a String into double value by just using the Double() constructor which will accept the string object as an argument, but we don’t recommend doing because it is deprecated.

Deprecated.
It is rarely appropriate to use this constructor. Use parseDouble(String) to convert a string to a double primitive, or use valueOf(String) to convert a string to a Double object.

public class Codekru {

	public static void main(String[] args) {

		String str = "1323.122d";
		double d = new Double(str);

		System.out.println("double value = " + d);

	}
}

Output –

double value = 1323.122

Using DecimalFormat

Well, so far, we have only converted some simple strings in a simple format. But what if the strings were in a different format ( like 1,323.122 ), then all of the above methods will fail to parse the string and convert it into a double value. This is where the DecimalFormat class comes into the picture as shown below

import java.text.DecimalFormat;
import java.text.ParseException;

public class Codekru {

	public static void main(String[] args) {

		try {
			String str1 = "1,323.122";
			String str2 = "-23,456";
			String str3 = "3456789";

			double d1 = DecimalFormat.getNumberInstance().parse(str1).doubleValue();
			double d2 = DecimalFormat.getNumberInstance().parse(str2).doubleValue();
			double d3 = DecimalFormat.getNumberInstance().parse(str3).doubleValue();

			System.out.println("d1 value is: " + d1);
			System.out.println("d2 value is: " + d2);
			System.out.println("d3 value is: " + d3);
		} catch (ParseException e) {
			e.printStackTrace();
		}

	}
}

Output –

d1 value is: 1323.122
d2 value is: -23456.0
d3 value is: 3456789.0

If you want to learn more about other Java conversions, then have a look at the articles here.

Reference – https://docs.oracle.com/javase/9/docs/api/java/lang/Double.html

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *