Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Convert String to Double in Java

  • Difficulty Level : Basic
  • Last Updated : 22 Apr, 2022

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

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

 

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 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

 

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 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

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!