Convert Double to Integer in Java
Given a Double real number, the task is to convert it into Integer in Java.
Examples:
Input: double = 3452.234 Output: 3452 Input: double = 98.23 Output: 98
- Using typecasting: This technique is a very simple and user friendly.
Syntax:
double data = 3452.345 int value = (int)data;
Example:
// Java program to convert Double to int// using TypecastingpublicclassGFG {// main methodpublicstaticvoidmain(String args[]){// Get the double valuedoubledata =3452.345;// convert into intintvalue = (int)data;// print the int valueSystem.out.println(value);}}Output:3452
- Using Math.round(): This method returns the nearest integer.
Syntax:
double data = 3452.645 int value = (int)Math.round(data);
Example:
// Java program to convert Double to int// using Math.round()publicclassGFG {// main methodpublicstaticvoidmain(String args[]){// Get the double valuedoubledata =3452.345;// convert into intintvalue = (int)Math.round(data);// print the int valueSystem.out.println(value);}}Output:3452
- Using Double.intValue(): This technique is similar to typecasting method. Wrapper class Double truncates all digits after decimal point.
Syntax:
double data = 3452.345 Double newData = new Double(data); int value = newData.intValue();
Example:
// Java program to convert Double to int// using using Double.intValue()publicclassGFG {// main methodpublicstaticvoidmain(String args[]){// Get the double valuedoubledata =3452.345;// Create a wrapper around// the double valueDouble newData =newDouble(data);// convert into intintvalue = newData.intValue();// print the int valueSystem.out.println(value);}}Output:3452
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.


