Skip to content
Related Articles

Related Articles

Improve Article

Convert String to Double in Java

  • Difficulty Level : Basic
  • Last Updated : 11 Dec, 2018


Given a String, the task is to convert it into Double in Java.

Examples:

Input: String = "20.156"
Output: 20.156

Input: String = "456.21"
Output: 456.21

We can use various methods to perform the task :

  1. Using Double.parseDouble()

    Syntax:

    double str1 = Double.parseDouble(str); 
    




    // Java program to convert String to Double
    // using parseDouble()
      
    public class GFG {
      
        // main method
        public static void main(String args[])
        {
      
            // create a String
            String str = "2033.12244";
      
            // convert into Double
            double str1 = Double.parseDouble(str);
      
            // print String as Double
            System.out.println(str1);
        }
    }
    Output:



    2033.12244
    
  2. Using Double.valueOf()

    Syntax:

    double str1 = Double.valueOf(str); 
    

    Examples:




    // Java program to convert String to Double
    // using parseDouble()
      
    public class GFG {
      
        // main method
        public static void main(String args[])
        {
      
            // create a String
            String str = "2033.12244";
      
            // convert into Double
            double str1 = Double.valueOf(str);
      
            // print String as Double
            System.out.println(str1);
        }
    }
    Output:
    2033.12244
    
  3. Using constructor of Double class

    Syntax:

    Double str1 = new Double(str); 
    

    Examples:




    // Java program to convert String to Double
    // using parseDouble()
      
    public class GFG {
      
        // main method
        public static void main(String args[])
        {
      
            // create a String
            String str = "2033.12244";
      
            // convert into Double
            Double str1 = new Double(str);
      
            // print String as Double
            System.out.println(str1);
        }
    }
    Output:
    2033.12244
    

    Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




    My Personal Notes arrow_drop_up
Recommended Articles
Page :