In this guide we will see how to convert String to Double in Java. There are three ways to convert String to double.
1. Java – Convert String to Double using Double.parseDouble(String) method
2. Convert String to Double in Java using Double.valueOf(String)
3. Java Convert String to double using the constructor of Double class – The constructor Double(String) is deprecated since Java version 9
1. Java Convert String to Double using Double.parseDouble(String)
public static double parseDouble(String str) throws NumberFormatException
This method returns the double representation of the passed String argument. This method throws NullPointerException
, if the specified String str is null and NumberFormatException
– if the string format is not valid. For example, if the string is “122.20ab” this method would throw NumberFormatException.
String str="122.202"; double dnum = Double.parseDouble(str);
The value of variable dnum
of double type would be 122.202 after conversion.
Lets see the complete example of the conversion using parseDouble(String) method.
Example 1: Java Program to convert String to double using parseDouble(String)
public class JavaExample{ public static void main(String args[]){ String str = "122.202"; /* Convert String to double using * parseDouble(String) method of Double * wrapper class */ double dnum = Double.parseDouble(str); //displaying the value of variable dnum System.out.println(dnum); } }
Output:
2. Java Convert String to Double using Double.valueOf(String)
The valueOf() method of Double wrapper class in Java, works similar to the parseDouble() method that we have seen in the above java example.
String str = "122.111"; double dnum = Double.valueOf(str);
The value of dnum
would be 122.111 after conversion
Lets see the complete example of conversion using Double.valueOf(String) method.
Example 2: Java Program to convert String to double using valueOf(String)
public class JavaExample{ public static void main(String args[]){ String str = "122.111"; /* Convert String to double using * valueOf(String) method of Double * wrapper class */ double dnum = Double.valueOf(str); //displaying the value of variable dnum System.out.println(dnum); } }
Output:
3. Java Convert String to double using the constructor of Double class
Note: The constructor Double(String) is deprecated since Java version 9
String str3 = "999.333"; double var3 = new Double(str3);
Double class has a constructor which parses the String argument that we pass in the constructor, and returns an equivalent double value.
public Double(String s) throws NumberFormatException
Using this constructor we can create a new object of the Double class by passing the String that we want to convert.
Example 3: Java Program to convert String to double using the constructor of Double class
In this example we are creating an object of Double class to convert the String value to double value.
public class Example{ public static void main(String args[]){ String str3 ="999.333"; double var3 = new Double(str3); System.out.println(var3); } }
Output:
999.333
Leave a Reply