Convert String to double in Java

Tags: String double Convert

In this Java core tutorial, we learn how to convert String value into double value in Java via different solutions.

Parse String to double using Double.parseDouble() method

ConvertStringToDoubleExample1.java

public class ConvertStringToDoubleExample1 {
    public static void main(String[] args) {
        String value1 = "12.343536";
        double value2 = Double.parseDouble(value1);

        System.out.println("String value: " + value1);
        System.out.println("double value: " + value2);
    }
}
The output is:
String value: 12.343536
double value: 12.343536

Using Double.valueOf() method to instantiate new double from a String

ConvertStringToDoubleExample2.java

public class ConvertStringToDoubleExample2 {
    public static void main(String[] args) {
        String value1 = "99.090909";
        double value2 = Double.valueOf(value1);

        System.out.println("String value: " + value1);
        System.out.println("double value: " + value2);
    }
}
The output is:
String value: 99.090909
double value: 99.090909

Happy Coding 😊