engma
engma

Reputation: 1959

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 ?

Upvotes: 1

Views: 3391

Answers (6)

Ajinkya
Ajinkya

Reputation: 22710

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.

enter image description here

Upvotes: 1

Harish Kumar
Harish Kumar

Reputation: 528

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

Upvotes: 0

Munesh
Munesh

Reputation: 1569

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.

enter image description here

Upvotes: 0

devnull
devnull

Reputation: 123498

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

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

Upvotes: 2

Tom Carchrae
Tom Carchrae

Reputation: 6486

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.

Upvotes: 2

BobTheBuilder
BobTheBuilder

Reputation: 19284

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));

Upvotes: 4

Related Questions