Java - Double parseDouble() method



Description

The Java Double parseDouble(String s) method returns a new double initialized to the value represented by the specified String, as performed by the valueOf method of class Double.

Declaration

Following is the declaration for java.lang.Double.parseDouble() method

public static double parseDouble(String s) throws NumberFormatException

Parameters

s − This is the string to be parsed.

Return Value

This method returns the double value represented by the string argument.

Exception

NumberFormatException − if the string does not contain a parsable double.

Example 1

The following example shows the usage of Double parseDouble(String) method to get a double value from a String. We've created a String with a valid double value and then using parseDouble() method, we're retrieving the double value and printing it.

package com.tutorialspoint;
public class DoubleDemo {
   public static void main(String[] args) {
      
      // returns the double value represented by the string argument
      String str = "50";
      double retval = Double.parseDouble(str);
      System.out.println("Value = " + retval);
   }
}  

Output

Let us compile and run the above program, this will produce the following result −

Value = 50.0

Example 2

The following example shows the usage of Double parseDouble(String) method to get a double value from a String. We've created a String with an invalid double value and then using parseDouble() method, we're trying to retrieve the double value and printing it. P)rogram throws an exception as expected.

package com.tutorialspoint;
public class DoubleDemo {
   public static void main(String[] args) {

      // returns the double value represented by the string argument
      String str = "abc";
      double retval = Double.parseDouble(str);
      System.out.println("Value = " + retval);
   }
}  

Output

Let us compile and run the above program, this will produce the following result −

Exception in thread "main" java.lang.NumberFormatException: For input string: "abc"
	at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
	at sun.misc.FloatingDecimal.parseDouble(Unknown Source)
	at java.lang.Double.parseDouble(Unknown Source)
	at com.tutorialspoint.DoubleDemo.main(DoubleDemo.java:9)

Example 3

The following example shows the usage of Double parseDouble(String) method to get a double value from a String. We've created a String with a valid negative double value and then using parseDouble() method, we're retrieving the double value and printing it.

package com.tutorialspoint;
public class DoubleDemo {
   public static void main(String[] args) {

      // returns the double value represented by the string argument
      String str = "-50";
      double retval = Double.parseDouble(str);
      System.out.println("Value = " + retval);
   }
}  

Output

Let us compile and run the above program, this will produce the following result −

Value = -50.0
java_lang_double.htm
Advertisements