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

JavaParsingInteger

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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.

Solution 3 - Java

Use,

String s="0.01";
int i= new Double(s).intValue();

Solution 4 - Java

String s="0.01";
int i = Double.valueOf(s).intValue();

Solution 5 - Java

This kind of conversion is actually suprisingly unintuitive in Java

Take for example a following string : "100.00"

C : a simple standard library function at least since 1971 (https://stackoverflow.com/questions/2909768/where-did-the-name-atoi-come-from)

int i = atoi(decimalstring);

Java : mandatory passage by Double (or Float) parse, followed by a cast

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

Java sure has some oddities up it's sleeve

Solution 6 - Java

Using BigDecimal to get rounding:

String s1="0.01";
int i1 = new BigDecimal(s1).setScale(0, RoundingMode.HALF_UP).intValueExact();

String s2="0.5";
int i2 = new BigDecimal(s2).setScale(0, RoundingMode.HALF_UP).intValueExact();

Solution 7 - Java

>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.

Solution 8 - Java

Use Double.parseDouble(String a) what you are looking for is not an integer as it is not a whole number.

Solution 9 - Java

use this one

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

Solution 10 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKevin BoydView Question on Stackoverflow
Solution 1 - JavaMartijn CourteauxView Answer on Stackoverflow
Solution 2 - JavaJorenView Answer on Stackoverflow
Solution 3 - JavaKV PrajapatiView Answer on Stackoverflow
Solution 4 - JavaSwapnil KadamView Answer on Stackoverflow
Solution 5 - JavaRaywellView Answer on Stackoverflow
Solution 6 - JavaAnnTeaView Answer on Stackoverflow
Solution 7 - Javaayushs27View Answer on Stackoverflow
Solution 8 - JavadominicView Answer on Stackoverflow
Solution 9 - JavaKevin TanView Answer on Stackoverflow
Solution 10 - JavaPrabsView Answer on Stackoverflow