How can I convert a String
such as "12.34"
to a double
in Java?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Used this to convert any String number to double when u need int just convert the data type from num and num2 to int ; took all the cases for any string double with Eng:"Bader Qandeel"
If you have problems in parsing string to decimal values, you need to replace "," in the number to "."
Using
Double.parseDouble()
without surroundingtry/catch
block can cause potentialNumberFormatException
had the input double string not conforming to a valid format.Guava offers a utility method for this which returns
null
in case your String can't be parsed.https://google.github.io/guava/releases/19.0/api/docs/com/google/common/primitives/Doubles.html#tryParse(java.lang.String)
Double valueDouble = Doubles.tryParse(aPotentiallyCorruptedDoubleString);
In runtime, a malformed String input yields
null
assigned tovalueDouble
To convert a string back into a double, try the following
The parseDouble method will achieve the desired effect, and so will the Double.valueOf() method.
Citing the quote from Robertiano above again - because this is by far the most versatile and localization adaptive version. It deserves a full post!
Another option:
You can use
Double.parseDouble()
to convert aString
to adouble
:For your case it looks like you want: