Convert String to Double in Java
Here, we will convert String to Double in Java. There are 3 methods for this conversion as mentioned below:
Illustration:
Input : String = "20.156" Output: 20.156
Input : String = "456.21" Output : 456.21
Different Ways for Converting String to Double
- Using the parseDouble() method of Double class
- Using valueOf() method of Double class
- Using constructor of Double class
Method 1: Using parseDouble() method of Double class
Syntax:
double str1 = Double.parseDouble(str);
Example:
Java
// Java program to convert String to Double// Using parseDouble() Method of Double Class// Main classpublic class GFG { // 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
Time Complexity: O(1) as constant operations are used.
Auxiliary Space: O(1) because no extra space is required.
Method 2: Using valueOf() method of Double class
Syntax:
double str1 = Double.valueOf(str);
Example:
Java
// Java program to convert String to Double// using valueOf() Method of Double Class// Main classpublic class GFG { // 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 str1 = Double.valueOf(str); // Printing above string as double type System.out.println(str1); }} |
Output:
2033.12244
Time Complexity: O(1) as constant operations are used.
Auxiliary Space: O(1) because no extra space is required.
Method 3: Using the constructor of Double class
Syntax:
Double str1 = new Double(str);
Example:
Java
// Java program to convert String to Double// Using Constructor of Double class// Main classpublic class GFG { // 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 str1 = new Double(str); // print above string as Double type System.out.println(str1); }} |
Output:
2033.12244
Time Complexity: O(1) as constant operations are used.
Auxiliary Space: O(1) because no extra space is required.


