Java program to convert string variables to double
Sure, here's a Java program that converts string variables to double:
refer ti:ogiftidea.compublic class StringToDoubleConverter { public static void main(String[] args) { String str1 = "3.14159"; String str2 = "2.71828"; double double1 = Double.parseDouble(str1); double double2 = Double.parseDouble(str2); System.out.println("double1: " + double1); System.out.println("double2: " + double2); } }
In this program, we first create two string variables: str1
and str2
.
We then use the Double.parseDouble
method to convert the strings to double values. This method takes a string as input and returns the equivalent double value.
Finally, we print out the double values of double1
and double2
to confirm that the conversion was successful.
Note that if the input string is not a valid double value (e.g. "hello" or "3.14.159"), the Double.parseDouble
method will throw a NumberFormatException
. It's important to handle this exception in your code if there's a possibility that the input string might not be a valid double value.