Convert string value to double
static double parseDouble(String s)
- Returns a double to the value represented by the String.
static Double valueOf(double d)
- Returns a Double instance representing the double value.
static Double valueOf(String s)
- Returns a Double object holding the double value represented by the argument string s.
public class Main {
public static void main(String[] args) {
System.out.println(Double.valueOf("1.234"));
}
}
The output:
1.234
Double.valueOf can also be used to check if a string is a number.
public class Main {
public static void main(String[] args) {
System.out.println(Double.valueOf("1.abc"));
}
}
The code above produces the following error message:
Exception in thread "main" java.lang.NumberFormatException: For input string: "1.abc"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1224)
at java.lang.Double.valueOf(Double.java:475)
at Main.main(Main.java:4)
You can use the Double.parseDouble in the same way
Home
Java Book
Essential Classes
Java Book
Essential Classes
Double:
- Double class
- Constants in Double class
- Double class Constructor
- Return double value as byte, double, float, int, long, short
- Compare two double values
- Is a double value an infinite large value, or it is not a number.
- Convert string value to double
- Convert double value from string
- Get the hexadecimal string representation
- Bit oriented calculation for double type