Convert String to double in Java
There are many ways in java through which String to Double conversion can be done. Today we will look into some common ways to convert java String to Double. Note that since java supports autoboxing, double primitive type and Double object can be used interchangeably without any issues.
1 2 |
Double d1 = 10.25d; //autoboxing from double to Double double d = Double.valueOf(10.25); //unboxing from Double to double |
Convert String to Double
Let’s have a look at all the different ways to convert string to double in java.
1. Convert using Double.parseDouble()
The Double.parseDouble()
is a static method that parses the string argument and returns a double value.
Syntax
1 |
public static double parseDouble(String s) throws NumberFormatException |
The parameter s will be converted to a primitive double value. Note that the method will throw a NumberFormatException if the parameter is not a valid double.
Example
1 2 3 |
String stringNum= "111.25"; double number = Double.parseDouble(stringNum); System.out.println("Number is: " + number); |
Output:
Number is: 111.25
This is the most common and popular solution when you need to convert a String to double. Note that the resulting value is not an instance of the Double class but just a plain primitive double value.
2. Convert using Double.valueOf()
The Double.valueOf()
is a static method that will return a Double object holding the value of the specified String.
Syntax
1 |
public static Double valueOf(String s) throws NumberFormatException |
Note that the method will throw a NumberFormatException if the parameter is not a valid double.
1 2 3 |
String stringNum= "111.25"; double number = Double.valueOf(stringNum); System.out.println("Number is: " + number); |
Output:
Number is: 111.25
This is the most common method when you wish to convert a String to Double. Note that the resulting value is an instance of the Double class and not a primitive double value.
3. Convert using new Double(String).doubleValue()
Another alternative method is to create an instance of Double class and then invoke it’s doubleValue() method.
Note that this constructor has been deprecated in Java 9
Example
1 2 |
String numberAsString = "111.25"; double number = new Double(stringNum).doubleValue(); |
Not a preferred method.
4. Convert using DecimalFormat
The class java.text.DecimalFormat
is a class that can be used to convert a number to it’s String representation. It can also be used the other way around – it can parse a String into it’s numerical representation.
Example
1 2 3 4 5 6 7 8 |
String stringNum= "111.25"; DecimalFormat decimalFormat = new DecimalFormat("#"); try { double number = decimalFormat.parse(stringNum).doubleValue(); System.out.println("Number is: " + number); } catch (ParseException e) { System.out.println("Not a valid number."); } |
Output:
Number is: 111.25
Note that parse() method returns instance of Number, so we are calling doubleValue() to get the double primitive type from it. Also this method throw ParseException if the string is not properly formatted.