Google News
logo
Java Program to convert string variables to double
In the following example of Java program to convert a String variable to a double :
Program :
public class Main {
    public static void main(String[] args) {
        String str = "3.14159";
        
        double d = Double.parseDouble(str); // convert string to double
        
        System.out.println("String: " + str);
        System.out.println("Double: " + d);
    }
}


In this program, we define a String variable str and assign it the value "3.14159". We then use the Double.parseDouble() method to convert this String to a double.

Output :
String: 3.14159
Double: 3.14159
Note that if the String value cannot be parsed as a double, a NumberFormatException will be thrown.