How to convert a double into a int?
Heaps of thanks for your kindly help :)
Printable View
How to convert a double into a int?
Heaps of thanks for your kindly help :)
by explicit casting.
Code:double x = 5.0;
int a = (int) x;
cast it into an int type like this:
double a = 5.89;
int b = (int)a;
if you do this, b = 5;
Thank you!
hi tyang,
below code can help you.
but one thing should be kept in your mind that converting form double 2 int will lost the pricision, some time, lost a lot.Code:public class Convert {
public static void main(String[] args) {
double x = 88.33;
int y = (int) x;
System.out.println(y);
}
}
take this code on your pc to see the result:
Code:public class Convert {
public static void main(String[] args) {
double x = Double.MAX_VALUE;
System.out.println(x);
int y = (int) x;
System.out.println(y);
}
}