Convert String to Double in Java


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

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    2033.12244
    
  2. Using Double.valueOf()

    Syntax:

    double str1 = Double.valueOf(str); 
    

    Examples:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    2033.12244
    
  3. Using constructor of Double class

    Syntax:

    Double str1 = new Double(str); 
    

    Examples:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    2033.12244
    


  4. My Personal Notes arrow_drop_up

    Strategy Path planning and Destination matters in success No need to worry about in between temporary failures

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.