Signup/Sign In

Java Double parseDouble() Method

Java parseDouble() method is the part of the Double class of the java.lang package. This method is used to parse the string value to its equivalent Double value. This method returns the same value as returned by the Double class' valueOf() method.

Syntax:

public static double parseDouble(String s) throws NumberFormatException

Parameters:

The parameter passed is the string value whose equivalent Double value is to be returned.

Returns:

Returns the equivalent double value corresponding to the string value passed as a parameter.

Exception:

NullPointerException - if the passed string is null

NumberFormatException - if the passed string does not contain a parsable double.

Example 1:

Here, two strings are passed and are returned as their respective equivalent Double value.

import java.lang.Double;
public class StudyTonight 
{  
    public static void main(String[] args) 
       {          
        String s1 = "21.62"; 
        String s2 = "-73.056";
        double n1 = Double.parseDouble(s1);  //converts the passed string into its corresponding double value
        double n2 = Double.parseDouble(s2);  //converts the passed string into its corresponding doube value        
        System.out.println("Equivalent Double value is : " + n1);
        System.out.println("Equivalent Double value is : " + n2);
        
       }  
} 


Equivalent Double value is : 21.62
Equivalent Double value is : -73.056

Example 2:

Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent output.

import java.lang.Double;
import java.util.Scanner;

public class StudyTonight 
{  
    public static void main(String[] args) 
      {  
         try
          {
            System.out.println("Enter Value");
            Scanner sc = new Scanner(System.in);
            String s = sc.nextLine();
            Double n = Double.parseDouble(s);  //converts the passed string as double value

            System.out.println("Equivalent Double value is : " + n);
          }
          catch(Exception e)
          {
            System.out.println("Invalid Input!!");
          }
      }  
}  


Enter Value
855.88
Equivalent Double value is : 855.88
****************************************
Enter Value
-445.6
Equivalent Double value is : -445.6
****************************************
Enter Value
0x554
Invalid Input!!

Live Example:

Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.



About the author:
A Computer Science and Engineering Graduate(2016-2020) from JSSATE Noida. JAVA is Love. Sincerely Followed Sachin Tendulkar as a child, M S Dhoni as a teenager, and Virat Kohli as an adult.