How to convert String into double in Java?

Views: 165   

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);
  2. Using Double.valueOf(String input) - This returns instance of Double type
    String input = "12345.67";
    Double output = Double.valueOf(input);
    Note: Code will through NumberFormatException if you do not pass a valid double String.
    Ex. if you pass "foo" that causes NumberFormatException.
On By