Convert Double Variable to Integer in Java

Exercise:

Write a Java Program to convert double type variables to int.

Click Here to View the Solution!
public class DoubleToInt {
   public static void main(String[] args) {
     double a = 12.897D;
     int c = (int)a;
 System.out.println(c);    
     }
 }
Click Here to View the Output!
12
Click Here to View the Explanation!
  • This program deals with transforming the values of double type variables into Integer type values.
  • In the first step, a variable of double type, by the name of a has been initialized with value 12.897D .
  • For the conversion purpose explicit type casting method is employed; that changes the data type of a given variables to a lower data type.
  • Explicit Type casting is performed by putting the data type in parenthesis, and keeping the variable alongside.
  • The respective value is assigned to int type variable c.
  • After that the value is displayed on the console.