[英]java.lang.NumberFormatException from parsing a string to a double
I cant figure it out 我不知道
double d = Double.valueOf(str);
double d2 = Double.parseDouble(str);
both methods produce the following exception: 这两种方法都会产生以下异常:
java.lang.NumberFormatException: Invalid double: "-73.04" java.lang.NumberFormatException:无效的双精度字符:“ -73.04”
However, the following hardcoded value works just fine: 但是,以下硬编码值可以正常工作:
double d = Double.valueOf("-73.04");
After copying the number from the title of this question and pasting into IntelliJ between double-quotes, it became clear what the problem is: 复制此问题标题中的数字并粘贴到双引号之间的IntelliJ中之后,就可以清楚地知道问题出在哪里:
double d = Double.parseDouble("\u200E-73.04");
Exception in thread "main" java.lang.NumberFormatException: For input string: "?-73.04"
Copying it from your hard-coded -73.04
does not have this behavior. 从您的硬编码-73.04
复制它没有此行为。
You have a "left-to-right mark" Unicode character in your string somehow. 您的字符串中以某种方式有一个“从左到右的标记” Unicode字符 。 You must eliminate that extraneous Unicode character from your string before parsing it. 解析字符串之前,必须从字符串中删除该多余的Unicode字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.