Convert String Variable to Double in Java

Exercise:

Write a Java Program to convert string variables to double.

Click Here to View the Solution!
public class StringToDouble {
  public static void main(String[] args) {
    String str1 = "8762";
    double num1 = Double.valueOf(str1);
    System.out.println(num1);    
  }
} 
Click Here to View the Output!
8762.0
Click Here to View the Explanation!
  • This program makes use of valueOf() method of Double class for the transformation of string values into double.
  • Similar to the previous program, a String type object str1 is  declared with value of 8762.0
  • Then this object is passed to valueOf() method as argument..
  • The converted value is stored into double type variable namely, num1.
  • Afterwards, num1 containing 8762.0 is printed.