Wednesday, July 3, 2024
HomeLanguagesJavaConvert String to Double in Java

Convert String to Double in Java

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:

  1. Using the parseDouble() method of the Double class
  2. Using the valueOf() method of Double class
  3. 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);
    }
}


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 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);
    }
}


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 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);
    }
}


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.

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments