1

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 ?

engma
  • 1,849
  • 2
  • 26
  • 55

6 Answers6

4

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));
BobTheBuilder
  • 18,858
  • 6
  • 40
  • 61
2

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.

Community
  • 1
  • 1
Tom Carchrae
  • 6,398
  • 2
  • 37
  • 36
2

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

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

devnull
  • 118,548
  • 33
  • 236
  • 227
1

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

Ajinkya
  • 22,324
  • 33
  • 110
  • 161
0

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

Munesh
  • 1,509
  • 3
  • 20
  • 46
0

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

Harish Kumar
  • 528
  • 2
  • 15