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

Viewed 245435

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?

10 Answers

suppose we take a integer in string.

String s="100"; int i=Integer.parseInt(s); or int i=Integer.valueOf(s);

but in your question the number you are trying to do the change is the whole number

String s="10.00";

double d=Double.parseDouble(s);

int i=(int)d;

This way you get the answer of the value which you are trying to get it.

One more solution is possible.

int number = Integer.parseInt(new DecimalFormat("#").format(decimalNumber))  

Example:

Integer.parseInt(new DecimalFormat("#").format(Double.parseDouble("010.021")))  

Output

10
Related