Convert String to Double in Java
In this article, we will convert a String to a Double in Java. There are three methods for this conversion from String to Double, as mentioned below in the article.
Methods for String-to-Double Conversion
Different ways for converting a String to a Double are mentioned below:
- Using the parseDouble() method of the Double class
- Using the valueOf() method of Double class
- Using the constructor of Double class
1. Using parseDouble() Method of Double Class
The parseDouble() method of the Java Double class is a built-in method in Java that returns a new double initialized to the value represented by the specified String, as done by the valueOf method of class Double.
Syntax:
double d = Double.parseDouble(str);
Example: Java Program to Convert String to Double Using parseDouble() Method.
// Java program to convert String to Double
// Using parseDouble() Method of Double Class
// Main class
public class Geeks
{
// Main driver method
public static void main(String args[])
{
// Create and initializing a string
String str = "2033.12244";
// Converting the above string into Double
// using parseDouble() Method
double str1 = Double.parseDouble(str);
// Printing string as Double type
System.out.println(str1);
}
}
Output
2033.12244
The complexity of the above method
- Time Complexity: O(1) as constant operations are used.
- Auxiliary Space: O(1) because no extra space is required.
2. Using valueOf() Method of Double Class
The doubleValue() method of Double class is a built-in method to return the value specified by the calling object as double after type casting.
Syntax:
double d = Double.valueOf(str);
Example: Java Program to Convert String to Double Using valueOf() Method.
// Java program to convert String to Double
// using valueOf() Method of Double Class
// Main class
public class Geeks
{
// Main driver method
public static void main(String args[])
{
// Creating and initializing a string
String str = "2033.12244";
// Converting the above string to Double type
double d = Double.valueOf(str);
// Printing above string as double type
System.out.println(d);
}
}
Output
2033.12244
The complexity of the above method
- Time Complexity: O(1) as constant operations are used.
- Auxiliary Space: O(1) because no extra space is required.
3. Using the Constructor of Double Class
The Double class contains the constructor to intialize the Double objects using a String object.
Syntax:
Double d = new Double(str);
Example: Java Program to Convert String to Double Using Double Class Constructor.
// Java program to convert String to Double
// Using Constructor of Double class
// Main class
public class Geeks
{
// Main driver method
public static void main(String args[])
{
// Creating and initializing a string
String str = "2033.12244";
// Converting above string into double type
Double d = new Double(str);
// print above string as Double type
System.out.println(d);
}
}
Output
2033.12244
The complexity of the above method
- Time Complexity: O(1) as constant operations are used.
- Auxiliary Space: O(1) because no extra space is required.