Table of Contents
With the introduction of different data types, the need for conversion of one data type to another has come into being. In this article, we are going to see how we can convert from a String data type into Double data type.
Conversion Modes
There are two ways in which String data can be converted into double. They are:
- Using the static method parseDouble(String) of the java.lang.Double wrapper class
- Using the static method valueOf(String)Â of the java.lang.Double wrapper class
- Using the constructor of Double wrapper class that takes String as its parameter
Let us see both the modes and how to do them.
parseDouble(String) method
The class java.lang.Double has a static method called parseDouble(String) that allows the programmer to easily convert a String containing decimal data into the corresponding Double value. The function returns a Double value.
Let us see a code snippet to illustrate the same:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package org.arpit.java2blog; public class ConvertStringToDouble { public static void main(String[] args) { String stringData = "35.126"; Double stringToDouble = Double.parseDouble(stringData); System.out.println(stringToDouble.doubleValue()); } } |
The above code on execution provides the following output:
What if String is not convertible to double
If String is not convertible to double, you will get below exception.
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:543)
at org.arpit.java2blog.java8.ConvertStringToDouble.main(ConvertStringToDouble.java:8)
Using valueOf(String) method
You can also use valueOf(String) method to convert String to double in java. You can call Double’s doubleValue() to convert it to primitive type.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package org.arpit.java2blog; public class ConvertStringToDouble { public static void main(String[] args) { String stringData = "35.126"; Double stringToDouble = Double.valueof(stringData); System.out.println(stringToDouble.doubleValue()); } } |
When you run above program, you will get below output.
Using the Double class constructor
The other method to convert to double is by using the parameterized constructor of java.lang.Double class that accepts a String as a parameter input. This creates an object of the Double class that stores the value of the String data sent and can be accessed using its doubleValue() function or by accessing the wrapper class object itself.
Let us see the code snippet:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package org.arpit.java2blog.java8; public class ConvertStringToDouble { public static void main(String[] args) { String stringData = "35.126"; Double stringToDouble = new Double(stringData); System.out.println(stringToDouble.doubleValue()); } } |
The above code on execution provides the following output:
That’s all about String to Double conversion.