将字符串解析为double/float抛出错误



我从UI作为字符串提取一对值,如1234,2456.00等。当我试图将这个字符串解析为float时,1234变成了1234.0,当我试图将其解析为双倍抛出错误时。我怎么解决这个问题?

我正在使用selenium web driver和java。以下是我尝试过的一些方法。

double Val=Double.parseDouble("SOQ");
double Val=(long)Double.parseDouble("SOQ");``

我认为你在试图弄清楚如何解析数字时把它弄混了一点。下面是一个概述:

// lets say you have two Strings, one with a simple int number and one floating point number
String anIntegerString = "1234";
String aDoubleString = "1234.123";
// you can parse the String with the integer value as  double 
double integerStringAsDoubleValue = Double.parseDouble(anIntegerString);
System.out.println("integer String as double value = " + integerStringAsDoubleValue);
// or you can parse the integer String as an int (of course)
int integerStringAsIntValue = Integer.parseInt(anIntegerString);
System.out.println("integer String as int value = " + integerStringAsIntValue);
 // if you have a String with some sort of floating point number, you can parse it as double
double doubleStringAsDoubleValue = Double.parseDouble(aDoubleString);
System.out.println("double String as double value = " + doubleStringAsDoubleValue);
// but you will not be able to parse an int as double
int doubleStringAsIntegerValue = Integer.parseInt(aDoubleString); // this throws a NumberFormatException because you are trying to force a double into an int - and java won't assume how to handle the digits after the . 
System.out.println("double String as int value = " + doubleStringAsIntegerValue);

这段代码将打印出:

integer String as double value = 1234.0
integer String as int value = 1234
double String as double value = 1234.123
Exception in thread "main" java.lang.NumberFormatException: For input string: "1234.123"

Java将停止"解析"数字的权利,当它击中.,因为一个整数永远不能有一个.,同样适用于任何其他非数字值,如"ABC", "123$", "one"…一个人可能能够将"123$"读成一个数字,但是Java不会对如何解释"$"做出任何假设。

此外:对于float或double,您可以提供一个正常的整数或任何带有.的地方,但除了.之外不允许使用其他字符(甚至不能使用,;,甚至不能使用WHITESPACE)

编辑:

如果你有一个数字的末尾有"零",这对人类来说可能看起来很好,也很容易理解,但计算机不需要它们,因为当省略零时,这个数字在数学上仍然是正确的。
例如:"123.00"123123.000000相同
这只是在再次打印或显示数字时格式化输出的问题(在这种情况下,数字将被转换回字符串)。你可以这样做:

    String numericString = "2456.00 ";  // your double as a string
    double doubleValue = Double.parseDouble(numericString);  // parse the number as a real double
      // Do stuff with the double value
    String printDouble = new DecimalFormat("#.00").format(doubleValue); // force the double to have at least 2 digits after the .
    System.out.println(printDouble);  // will print "2456.00"

你可以在这里找到DecimalFormat的概述。

例如#表示"这是一个数字,但前导零被省略"0表示"这是一个数字,即使为零也不会被省略"

希望能有所帮助

你的第一个问题是"SOQ"不是一个数字。

第二,如果你想用String创建一个数字,你可以用parseDouble并给出一个没有小数点的值。像这样:

Double.parseDouble("1");

如果你有一个值保存为long,你不需要做任何转换将其保存为双精度类型。这将编译并打印10.0:

long l = 10l;
double d = l;
System.out.println(d);

最后,请阅读这个问一个好问题

问题是您不能将非数字输入解析为Double

例如:

Double.parseDouble("my text");
Double.parseDouble("alphanumeric1234");
Double.parseDouble("SOQ");

将导致错误。

,但以下是有效的:

Double.parseDouble("34");
Double.parseDouble("1234.00");

要解析为Double的数字包含","和空格,因此在进行解析之前需要先去掉它们

String str = "1234, 2456.00".replace(",", "").replace(" ", "");
double Val=Double.parseDouble(str);

最新更新