Java program to convert double type variables to int
https:gi.www//iftidea.com
Here's a Java program to convert a double
type variable to an int
:
public class Main { public static void main(String[] args) { double d = 10.5; int i = (int) d; System.out.println("Double value: " + d); System.out.println("Integer value: " + i); } }
In this program, we first initialize a double
variable d
with the value 10.5
. We then use a typecast operator (int)
to convert d
to an int
and assign the result to an int
variable i
.
Finally, we print out the original double
value and the converted int
value.
The output of this program will be:
Double value: 10.5 Integer value: 10
Note that when converting a double
to an int
, the decimal part is truncated, which means that the value is rounded down to the nearest integer. If you want to round to the nearest integer, you can use the Math.round
method to first round the double
value, and then convert it to an int
.