String s = "0.01";
double d = Double.parseDouble(s);
int i = (int) d;
The reason for the exception is that an integer does not hold rational numbers (= basically fractions). So, trying to parse 0.3
to a int is nonsense.
A double
or a float
datatype can hold rational numbers.
The way Java casts a double
to an int
is done by removing the part after the decimal separator by rounding towards zero.
int i = (int) 0.9999;
i
will be zero.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…