java 如何将字符串转换为双精度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7239542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I convert string to double?
提问by user887738
I am developing an android app. I need to convert string to double. How can I do it?
我正在开发一个 android 应用程序。我需要将字符串转换为双精度。我该怎么做?
回答by Kartik Domadiya
You just need this :
你只需要这个:
double d=Double.parseDouble(myString);
If your String
value cannot be parsed to double it will throw NumberFormatException
. So better you put the parseDouble
statement inside try catch block.
如果您的String
值无法解析为双倍,它将抛出NumberFormatException
. 所以最好将parseDouble
语句放在 try catch 块中。
回答by Pratik
here is the example
这是例子
try{
double dbl = Double.parseDouble("99.882039");
catch(NumberFormatException ex){
// handle exception
}
回答by Thilo
double d = Double.parseDouble(theString);
回答by leifg
That's not a thing of android but if you're sure you string can be expressed as a double you can do it like this with simple Java:
这不是 android 的事情,但是如果您确定您的字符串可以表示为 double,您可以使用简单的 Java 这样做:
Double.valueOf("1.2");