Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I just wants to convert from double to int in my UI i am rendering as a double but for the backend i want convert to integer.

Double d = 45.56;

OutPut = 4556;

Please can anybody tell me how to get the value in this format.

like image 407
Anshuman Patnaik Avatar asked Jul 23 '15 10:07

Anshuman Patnaik


3 Answers

Try this way, Courtesy

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

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

like image 138
IntelliJ Amiya Avatar answered Oct 29 '22 17:10

IntelliJ Amiya


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 String
String str = str.replace(".", "");
//Convert the string back to int
int i = Integer.parseInt(str);
// here i becomes 4556
like image 31
Prokash Sarkar Avatar answered Oct 29 '22 17:10

Prokash Sarkar


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());

EDIT If 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;
like image 2
Sanjeet A Avatar answered Oct 29 '22 17:10

Sanjeet A