java Double.parseDouble() 给出“多点,NumberFormatException”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35059473/
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
Double.parseDouble() gives "Multiple Points, NumberFormatException"
提问by Saikiran Gosikonda
I have to compare two Strings by parsing them as Double
.
The Strings are: "2.1.10" and "2.1.9". Here, I need to know whether the first one is greater than second one or not?
But, while parsing these usingDouble.parseDouble("2.1.10")
or Double.parseDouble("2.1.9")
it is giving the NullPointerException saying "Multiple Points".
我必须通过将它们解析为Double
.
字符串是:“2.1.10”和“2.1.9”。在这里,我需要知道第一个是否大于第二个?
但是,在使用Double.parseDouble("2.1.10")
or解析这些时,Double.parseDouble("2.1.9")
它给出了 NullPointerException 说“多点”。
回答by Jordi Castilla
You don't compare doubles, you compare version numbers, and this look but are not numbers, you must compare each position.
你不比较双打,你比较版本号,这看起来但不是数字,你必须比较每个位置。
Something like:
就像是:
String[] v1 = "2.1.9".split("\.");
String[] v2 = "2.1.10".split("\.");
if (v1.length != v2.length)
return;
for (int pos = 0; pos < v1.length; pos++) {
// compare v1[pos] with v2[pos] as necessary
if (Integer.parseInt(v1[pos]) > Integer.parseInt(v2[pos])) {
System.err.println("v1 is greater");
} else if (Integer.parseInt(v1[pos]) < Integer.parseInt(v2[pos])) {
System.err.println("v2 is greater");
}
}
RESULT:
结果:
v2 is greater
回答by William Dunne
The reason is because you have two decimal places in your number, which isn't allowed in maths. Remove one of the decimals from your string and it will work.
原因是你的数字有两位小数,这在数学中是不允许的。从您的字符串中删除一个小数,它会起作用。
If you are using version numbers, break it down into three numbers and work along, like you would if you were checking alphabetical order.
如果您使用的是版本号,请将其分解为三个数字并继续工作,就像检查字母顺序一样。
回答by Vineet kaushik
2.1.10 can never be mathematical number hence it is giving NumberFormatException. and even if you pass blank to Double.parseDouble("") still throw the same NumberFormatException.
2.1.10 永远不可能是数学数字,因此它给出了 NumberFormatException。即使您将空白传递给 Double.parseDouble("") 仍然会抛出相同的 NumberFormatException。
you rather store "2.1.10" in String only instead of into any number type and let me know if you have any other option.
您宁愿将“2.1.10”存储在字符串中,而不是存储在任何数字类型中,如果您有任何其他选择,请告诉我。
回答by BlueSlimShady
It is not possible as your value has two '.' and java does not check two decimal points. Remove one and then try. It is recommended to break down the value into two like for example 2.1.10 || 2.1.9 first check if 2.1 is the same.then check if 10/9 is same or greater. Hope this helped
这是不可能的,因为您的值有两个 '.' 而java不检查两个小数点。删除一个然后尝试。建议将值分解为两个,例如 2.1.10 || 2.1.9 首先检查 2.1 是否相同。然后检查 10/9 是否相同或更大。希望这有帮助
回答by Minudika Malshan Gammanpila
Since Double represents a numeric value, it should be mathematically correct. In other words there can be only one decimal place in a number.
由于 Double 表示一个数值,所以它在数学上应该是正确的。换句话说,一个数字只能有一位小数。
Therefore
Double.parseDouble("123.12")
will execute properly
and
Double.parseDouble("123.1.2")
will throw the exception which you've already got.
因此
Double.parseDouble("123.12")
将正确执行并
Double.parseDouble("123.1.2")
抛出您已经得到的异常。