String To Double In Java
Chapter:
String Handling
Last Updated:
13-08-2016 13:24:02 UTC
Program:
/* ............... START ............... */
public class JavaStringToDouble {
public static void main(String args[]) {
// Using parseDouble
String string = "344.47";
Double doubleVal = Double.parseDouble(string);
System.out.println(doubleVal);
Double doubleVal1 = new Double(string);
System.out.println(doubleVal1);
// Using valueOf method
Double doubleVal2 = Double.valueOf(string);
System.out.println(doubleVal2);
}
}
/* ............... END ............... */
Output
Notes:
-
The Double.parseDouble() static method parses the string argument and returns a double value. The parameters will be converted to a primitive double value.
- Syntax : public static double parseDouble(String s) throws NumberFormatException.
- The Double.valueOf() static method will return a Double object holding the value of the specified String.
- Syntax : public static Double valueOf(String s) throws NumberFormatException.
Tags
String To Double, Java