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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:14:33  来源:igfitidea点击:

How can I convert string to double?

javaandroidstringdouble

提问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 Stringvalue cannot be parsed to double it will throw NumberFormatException. So better you put the parseDoublestatement 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");