Exercise:
Write a Java Program to convert string variables to double.
Click Here to View the Solution!
public class StringToDouble {
public static void main(String[] args) {
String str1 =
"8762";
double num1 = Double.valueOf(str1);
System.out.println(num1);
}
}
Click Here to View the Output!
8762.0
Click Here to View the Explanation!
- This program makes use of
valueOf() method
ofDouble
class for the transformation ofstring
values intodouble
. - Similar to the previous program, a
String
type objectstr1
is declared with value of8762.0
- Then this object is passed to
valueOf() method
as argument.. - The converted value is stored into
double
type variable namely,num1
. - Afterwards,
num1
containing8762.0
is printed.