6

How to convert string value into int? I am getting number format exception.

String s = "20.00";
int i = (Integer.parseInt(s));
System.out.println(i);

Result should be like i=20.

2
  • If there's a decimal in your string, I assume that it may be possible to have an actual decimal after it? In the case of 20.60 what do you expect the integer to be? Commented Oct 25, 2013 at 12:50
  • @BBdev Actually, OP's code is the answer on that question. That question says nothing about decimals in the String. Commented Oct 25, 2013 at 12:56

2 Answers 2

12

What about:

int i = (int) Double.parseDouble(s);

Of course, "20.00" is not in a valid integer format.

Sign up to request clarification or add additional context in comments.

Comments

2

String s = "20.00";

is not valid Integer value that is the reason its throwing NumberFormatException.

Format your number using either Double or Float then using narrow casting cast you number to int but you may loose precision if exists.

i.e. int I = (int) Double.parseDouble(str);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.