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.
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 String
String str = str.replace(".", "");
//Convert the string back to int
int 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());
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With