Here, we will convert String to Double in Java. There are 3 methods for this Conversion from String to Double as mentioned below:
Example of String to Double Conversion
Input : String = “20.156”
Output: 20.156
Input : String = “456.21”
Output : 456.21
Methods for String to Double Conversion
Different Ways for Converting String to 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 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 str1 = Double.parseDouble(str);
Java Program to Convert String to Double Using parseDouble() Method
Java
public class GFG {
public static void main(String args[])
{
String str = "2033.12244" ;
double str1 = Double.parseDouble(str);
System.out.println(str1);
}
}
|
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 str1 = Double.valueOf(str);
Java Program to Convert String to Double Using valueOf() Method
Java
public class GFG {
public static void main(String args[])
{
String str = "2033.12244" ;
double str1 = Double.valueOf(str);
System.out.println(str1);
}
}
|
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 str1 = new Double(str);
Java Program to Convert String to Double Using Double Class Constructor
Java
public class GFG {
public static void main(String args[])
{
String str = "2033.12244" ;
Double str1 = new Double(str);
System.out.println(str1);
}
}
|
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.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
04 Aug, 2023
Like Article
Save Article