简体   繁体   中英

NumberFormatException when parsing a Double from string

Long story short here is my code that throws the number format exception:-

String percenta = "6.415‏";
holder.setPercent(Double.parseDouble(percenta));

I've tried a also Double.valueOf() but none of them work, still throws the same exception

here is the exception message

java.lang.NumberFormatException: For input string: "6.415‏"

what seems to be the problem here ?

EDIT

so the problem is in my encoding after all, how do i change that, I am using JSOUP to parse a page and it is supposed to be UTF-8, is Double.parseDouble not UTF-8 friendly ?

Try using:

NumberFormat format = NumberFormat.getInstance(Locale.US);
Number number = format.parse("6.415");
double d = number.doubleValue();

This may happen because of your internal Locale specification.

In order to verify it, check if it works:

String percenta = "6,415‏";
holder.setPercent(Double.parseDouble(percenta));

I'd guess it is because your computer is running a non-English locale. Try Double.valueOf("6.415") for the English locale, or see Best way to parseDouble with comma as decimal separator? for more info on parsing.

The choice of variable name percenta suggests that you're using the locale sk-SK .

The decimal separator for the locale is , and not . .

6.415 is a valid value and should not throw a NumberFormatException .
Check your encoding. When I copy-pasted your code, got error while saving it.

在此处输入图片说明

The encoding for the 'percenta = "6.415‏"' is not supported for Double to parse.

Please check the below error when I tried to copy your code.

在此处输入图片说明

The problem is with the 6.415. It seems you are using keyboard that is not having compliant character encoding.

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