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);}}chevron_rightfilter_noneOutput: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);}}chevron_rightfilter_noneOutput: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);}}chevron_rightfilter_noneOutput:3452
- Program to Convert Set of Integer to Array of Integer in Java
- Convert String to Double in Java
- Program to Convert Set of Integer to Set of String in Java
- Program to convert set of String to set of Integer in Java
- Java Guava | Doubles.indexOf(double[] array, double target) method with Examples
- Java Guava | Doubles.indexOf(double[] array, double[] target) method with Examples
- Program to convert List of String to List of Integer in Java
- Program to convert List of Integer to List of String in Java
- Java.Lang.Double class in Java
- Double Brace Initialization in Java
- Double colon (::) operator in Java
- Double longValue() in Java with Examples
- Double floatValue() in Java with Examples
- OptionalDouble of(double) method in Java with examples
- Double doubleToLongBits() method in Java with examples
Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



