How to convert String into double in Java?


There are two ways you can convert a String into double in Java

  1. Using Double.parseDouble(String input) - This returns primitive double
String input = "12345.67";
double output = Double.parseDouble(input);
System.out.println(output);
  1. Using Double.valueOf(String input) - This returns instance of Double type
String input = "12345.67";
Double output = Double.valueOf(input);
System.out.println(output);

Note: Code will through NumberFormatException if you do not pass a valid double String.
Ex. if you pass "foo" that causes NumberFormatException.