javaparsinginteger

How to do an Integer.parseInt() for a decimal number?


The Java code is as follows:

String s = "0.01";
int i = Integer.parseInt(s);

However this is throwing a NumberFormatException... What could be going wrong?


Solution

  • 0.01 is not an integer (whole number), so you of course can't parse it as one. Use Double.parseDouble or Float.parseFloat instead.