Convert String to double in Java

Last Updated On Tuesday 5th Oct 2021

Java Convert String to double

How to Convert String to Double Java.

	String text = "12.34"; // example String

OutPut:  12.34
	

Double.valueOf()

You can use Double.valueOf() to convert a String to a double.

The java.lang.Double.valueOf(String str) method returns a Double object holding the double value represented by the argument string str.

	double out = Double.valueOf(inp);
	
	public class main {

    public static void main(String args[])
    {

        String inp = "245.754";

        // convert into Double
        double out = Double.valueOf(inp);

        System.out.println(out);
    }
}
	

Double.parseDouble()

You can use Double.parseDouble() to convert a String to a double.
The parseDouble(String str) method of Java Double class returns a new double value that is initialized to the value corresponding to defined String str.

	double out = Double.parseDouble(inp);
	
	public class main {

    public static void main(String args[])
    {

        String inp = "123.44";

        // convert into Double
        double out = Double.parseDouble(inp);

        System.out.println(out);
    }
}
	

References