Convert Java String to Double

There are cases when we get a String that is actually a double. To work with it, we need to convert it to a double data type. Let’s see how we can convert String to Double in Java.

Convert Java String to Double with parseDouble() method

In the below example, we use the parseDouble() static method of a Double class to parse Java String to double.

class Test {

  public static void main(String[] args) {
    double d1 = Double.parseDouble("121,4");
    double d2 = Double.parseDouble("21,3");
    double d3 = Double.parseDouble("2831,9");

    System.out.println(d1);
    System.out.println(d2);
    System.out.println(d3);
  }
}
Output: 121.4 21.3 2831.9
 
We have previously discussed the opposite conversion in another tutorial: Convert Double to String in Java. Be sure to check it out for more information.
 
That’s all about how to convert String to double in Java. Proceed to the next lesson.
 
Happy coding!

Leave a Reply

Your email address will not be published.