## Java, J2EE & SOA Certification Training

- 33k Enrolled Learners
- Weekend
- Live Class

Since the time we have started understanding numbers, each one of us is inquisitive about converting numbers from one form to the other. One of the most popular conversions is converting a number from Double to Int. But, in an environment where hundreds of numbers need to be converted, it’s almost next to impossible to do it manually. So, instead, we can just write a simple code on how to convert Double to Int in Java. Hence in this article, I will discuss the same, in the following sequence:

Before I discuss the programming way of converting Double values to integers in Java, let us discuss the different ways provided by Java.

As we all know, a Double primitive contains decimal digits. On conversion of these values to integers, the decimal digits get truncated, by rounding off the number to the nearest integer according to the method you choose. Java provides the following three ways to convert Double values to integer numbers:

Refer to the below table to get a gist about the above methods.

Since you have understood gist of all the three methods, let us understand how to write a code for it.

This method is used to down-cast the Double value to an integer.

double var = double value; //Assign double value to the variable var int newvar = (int)var; //Assign the converted integer value to variable newvar

package edureka; import java.util.Scanner; public class DoubleToIntExample { public static void main(String[] args) { Scanner Input = new Scanner (System.in); System.out.print("Enter a Number with decimal digits greater than 5 - "); double Number = Input.nextDouble(); int IntNumber = (int)Number; System.out.println("The decimal number with decimal digits greater than 5 is convereted to integer - " +IntNumber); System.out.print("Enter a Number with decimal digits less than 5 - "); double Number1 = Input.nextDouble(); int IntNumber1 = (int)Number1; System.out.println("The decimal number with decimal digits less than 5 is convereted to integer - " +IntNumber1); System.out.print("Enter a Number with decimal digits equal to 5 - "); double Number2 = Input.nextDouble(); int IntNumber2 = (int)Number2; System.out.println("The decimal number with decimal digits equal to 5 is convereted to integer - " +IntNumber2); } }

Next, let us see how to convert Double to Int in Java, by using the math.round() method.

This method is used to round the Double value to the nearest integer.

double var = double value; //Assign double value to the variable var int newvar = (int)Math.round(var); //Assign the converted integer value to variable newvar

package edureka; import java.util.Scanner; public class DoubleToIntExample { public static void main(String[] args) { Scanner Input = new Scanner (System.in); System.out.print("Enter a Number with decimal digits greater than 5 - "); double Number = Input.nextDouble(); int IntNumber = (int)Math.round(Number); System.out.println("The decimal number with decimal digits greater than 5 is convereted to integer - " +IntNumber); System.out.print("Enter a Number with decimal digits less than 5 - "); double Number1 = Input.nextDouble(); int IntNumber1 = (int)Math.round(Number1); System.out.println("The decimal number with decimal digits less than 5 is convereted to integer - " +IntNumber1); System.out.print("Enter a Number with decimal digits equal to 5 - "); double Number2 = Input.nextDouble(); int IntNumber2 = (int)Math.round(Number2); System.out.println("The decimal number with decimal digits equal to 5 is convereted to integer - " +IntNumber2); } }

Next, let us see how to convert double to Int in Java, by using the Double.intValue() method.

This method is used when you have a double-object.

double var = double value; //Assign double value to the variable var Double newvar = new Double(var); //Double object int var1 = newvar.intValue; //Assign the converted integer value to variable var1

package edureka; import java.util.Scanner; public class DoubleToIntExample { public static void main(String[] args) { Scanner Input = new Scanner (System.in); System.out.print("Enter a Number with decimal digits greater than 5 - "); double Number = Input.nextDouble(); Double DNumber = new Double(Number); int IntNumber = DNumber.intValue(); System.out.println("The decimal number with decimal digits greater than 5 is convereted to integer - " +IntNumber); System.out.print("Enter a Number with decimal digits less than 5 - "); double Number1 = Input.nextDouble(); Double DNumber1 = new Double(Number1); int IntNumber1 = DNumber1.intValue(); System.out.println("The decimal number with decimal digits less than 5 is convereted to integer - " +IntNumber1); System.out.print("Enter a Number with decimal digits equal to 5 - "); double Number2 = Input.nextDouble(); Double DNumber2 = new Double(Number2); int IntNumber2 = DNumber2.intValue(); System.out.println("The decimal number with decimal digits equal to 5 is convereted to integer - " +IntNumber2); } }

**Output:**

*If you found this article on “How to Convert Double to Int in Java?”, check out the Java Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. We are here to help you with every step on your journey, for becoming a besides this java interview questions, we come up with a curriculum which is designed for students and professionals who want to be a Java Developer. *

*Got a question for us? Please mention it in the comments section of this “How to Convert Double to Int in Java*”* and we will get back to you as soon as possible.*