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?
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?
0.01 is not an integer (whole number), so you of course can't parse it as one. Use Double.parseDouble instead, or Float.parseFloat.
String s = "0.01";
double d = Double.parseDouble(s);
int i = (int) d;
The reason for the exception is that an integer isn't decimal.
A double or a float are a decimal numbers. An int not. You have to know by yourself what you need.
The way Java cast an double
to a int
: remove the decimal piece
.
int i = (int) 0.99d;
i
will by zero.
Use Double.parseDouble(String a) what you are looking for is not an integer as it is not a whole number.