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.156Input : 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
// Java program to convert String to Double // Using parseDouble() Method of Double Class // Main class public 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); } } |
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 str1 = Double.valueOf(str);
Java Program to Convert String to Double Using valueOf() Method
Java
// Java program to convert String to Double // using valueOf() Method of Double Class // Main class public 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); } } |
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 str1 = new Double(str);
Java Program to Convert String to Double Using Double Class Constructor
Java
// Java program to convert String to Double // Using Constructor of Double class // Main class public 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); } } |
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.