简体   繁体   中英

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"

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:

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.

You have a "left-to-right mark" Unicode character in your string somehow. You must eliminate that extraneous Unicode character from your string before parsing it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM