string to double java

www.lau‮moc.irutt‬
string to double java

To convert a string to a double value in Java, you can use the Double.parseDouble method. This method takes a string as an argument and returns the double value represented by that string.

Here's an example of how to use the Double.parseDouble method:

String s = "3.14";
double d = Double.parseDouble(s);  // d is 3.14

Note that the Double.parseDouble method only works for strings that represent a valid double value. If the input string is empty or contains non-numeric characters, the Double.parseDouble method will throw a NumberFormatException.

Here's an example of how you can handle the NumberFormatException:

String s = "3.14";
try {
    double d = Double.parseDouble(s);
} catch (NumberFormatException e) {
    e.printStackTrace();
}

It's a good idea to handle this exception in a try-catch block, as shown in the example above, to avoid your program crashing if the input string is not a valid representation of a double value.

Created Time:2017-10-17 20:18:55  Author:lautturi