Java - Convert String to double
To convert a String to double in Java, we can use:
Double.parseDouble()
static double parseDouble(String s): Returns a new double initialized to the value represented by the specified String, as performed by the valueOf method of class Double.
double d1 = Double.parseDouble("99.90");
System.out.println(d1); // 99.9
double d2 = Double.parseDouble("99.901");
System.out.println(d2); // 99.901
double d3 = Double.parseDouble("99.9010D");
System.out.println(d3); // 99.901
double d4 = Double.parseDouble("-99.909d");
System.out.println(d4); // -99.909
Double.valueOf()
static Double valueOf(double d): Returns a Double instance representing the specified double value.
double d1 = Double.valueOf("88.80");
System.out.println(d1); // 88.9
double d2 = Double.valueOf("88.801");
System.out.println(d2); // 88.801
double d3 = Double.valueOf("88.8010d");
System.out.println(d3); // 88.801
double d4 = Double.valueOf("-88.808D");
System.out.println(d4); // -88.808
Implicit casting automatically unbox Double into double.
Deprecated: Double‘s Constructor
Double(String s) is deprecated.
It is rarely appropriate to use this constructor. Use parseDouble(String) to convert a string to a double primitive, or use valueOf(String) to convert a string to a Double object.
double d1 = new Double("123");
System.out.println(d1); // 123
double d2 = new Double("-88.808D");
System.out.println(d2); // -88.808D
double d3 = new Double("FF00"); // NumberFormatException
System.out.println(d3);
DecimalFormat.parse()
parse(String source) throws ParseException: Parses text from the beginning of the given string to produce a number. The method may not use the entire text of the given string.
import java.text.DecimalFormat; import java.text.ParseException;
DecimalFormat decimalFormat = new DecimalFormat();
try {
double l = decimalFormat.parse("456.0990D").doubleValue();
System.out.println(l); // 456.99
} catch (ParseException e) {
System.out.println(e.getMessage());
}
We also can use external libraries like Apache Commons NumberUtils, Spring's NumberUtils, and Google's Guava primitive Doubles.
Apache Commons NumberUtils
- static double toDouble(String str): Convert a String to a double, returning 0.0d if the conversion fails.
- static double toDouble(String str, double defaultValue): Convert a String to a double, returning a default value if the conversion fails.
- static Double createDouble(String str): Convert a String to a Double.
import org.springframework.util.NumberUtils;
double d1 = NumberUtils.toDouble("365.536");
System.out.println(d1); // 365.536
double d2 = NumberUtils.toDouble("");
System.out.println(d2); // 0.0
double d3 = NumberUtils.toDouble("365.5360D", 0);
System.out.println(d3); // 365.536
double d4 = NumberUtils.toDouble("xyz", -1);
System.out.println(d4); // -1.0
double d5 = NumberUtils.toDouble("", -1);
System.out.println(d5); // -1.0
double d6 = NumberUtils.createDouble("365.5360d");
System.out.println(d6); // 365.536
double d7 = NumberUtils.createDouble("-#88FF"); // NumberFormatException
System.out.println(d7);
Spring NumberUtils
Similar like in Converting String to int and Converting String to long, we can use Spring's NumberUtils to parse String to number (in this case double).
import org.springframework.util.NumberUtils;
double d1 = NumberUtils.parseNumber("95.085", Double.class);
System.out.println(d1); // 95.085
double d2 = NumberUtils.parseNumber("-31.490", Double.class);
System.out.println(d2); // -31.49
Google Guava Doubles.tryParse()
static Double tryParse(String string): Parses the specified string as a double-precision floating point value.
import com.google.common.primitives.Longs;
double d1 = Doubles.tryParse("512.345");
System.out.println(d1); // 512.345
double d2 = Doubles.tryParse("-512.3450D");
System.out.println(d2); // -512.345
Also check on How to Handle NumberFormatException in Java