How To Convert Double To Int in Java?

Last updated on Nov 27,2019 38K Views

How To Convert Double To Int in Java?

edureka.co

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:

      1. TypeCasting
      2. Math.round()
      3. Double.intValue()

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

Ways to convert Double to Int in 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:

    1. TypeCasting
    2. Math.round()
    3. Double.intValue()

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

TypeCastingMath.round()Double.intValue()
Easy and user-friendly. It is used when your aim is to get rid of the numbers present after the decimal point.
This method is used to round the Double value to the nearest integerIt is used when you have a Double-object.
Example:

int value = (int) 3.89;

Output: 3

Example:

int value = (int) Math.round (3.89);

Output: 4

Example:

double d = 3.89;
int i = d.intValue();

Output: 3

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

Convert Double to Int in Java using TypeCasting

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

Syntax:

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

Example:

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); 
	}

}

Output:

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

Convert Double to Int in Java using Math.round()

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

Syntax:

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

Example:

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); 
	}

}

Output:

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

Convert Double to Int in Java using Double.intValue()

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

Syntax:

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 

Example:

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.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 1st March,2025

1st March

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES
REGISTER FOR FREE WEBINAR Building Robust Applications with Spring Framework