How to convert double to int in Android? [duplicate] How to convert double to int in Android? [duplicate] android android

How to convert double to int in Android? [duplicate]


Try this way, Courtesy

double d = 45.56;int i = (int) d;

For better info you can visit converting double to integer in java


If you just want to convert the Double to int,

Double D = 45.56;int i = Integer.valueOf(D.intValue());//here i becomes 45

But if you want to remove all decimal numbers and count the whole value,

//first convert the Double to String double D = 45.56;String s = String.valueOf(D);// remove all . (dots) from the StringString str = str.replace(".", "");//Convert the string back to intint i = Integer.parseInt(str);// here i becomes 4556


You are using the Double as object(Wrapper for type double). You need to fist convert it in to string and then int.

Double d=4.5;int  i = Integer.parseInt(d.toString());

If you want it in the Integer Object Wrapper then can be written as

Integer  i = Integer.parseInt(d.toString());

EDITIf you want to get the desired result -You can go like this-

    Double d = 4.5;    double tempD = d;    int tempI = (int) tempD * 100;    //Integer i = tempI;