简体   繁体   English

将字符串解析为double的java.lang.NumberFormatException

[英]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.

相关问题 java.lang.NumberFormatException:对于输入字符串:“”解析字符串表达式 - java.lang.NumberFormatException: For input string: “” parsing string expression 将String输入转换为Double并避免java.lang.NumberFormatException - Convert String input to Double and avoid java.lang.NumberFormatException java.lang.numberformatexception 解析字符串输入数据库时 - java.lang.numberformatexception when parsing string to input into database Double.valueOf()java.lang.NumberFormatException - Double.valueOf() java.lang.NumberFormatException java.lang.NumberFormatException:无效的int:“”(正在解析) - java.lang.NumberFormatException: Invalid int: “” (parsing) java.lang.numberformatexception:无效双:“” - java.lang.numberformatexception: invalid double: " " 从JTable到BigDecimal的字符串值将引发Java.lang.NumberFormatException - String value from a JTable to BigDecimal throws Java.lang.NumberFormatException java.lang.NumberFormatException用于将字符串转换为Java - java.lang.NumberFormatException for String to Int Java java.lang.NumberFormatException:对于输入字符串:“1” - java.lang.NumberFormatException: For input string: " 1" 错误java.lang.NumberFormatException:对于输入字符串:“” - Error java.lang.NumberFormatException: For input string: “”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM