Signup/Sign In

How to convert Java double to int

In Java, a double can be converted into an int using the typecast operator. This is because, in order to convert a higher data type into a lower data type, typecasting needs to be performed and this type of conversion is called Typecasting conversion.

In Java, Typecasting is performed through the typecast operator (datatype).

Example 1:

Here, the primitive double value is converted into int using the typecasting operator.

public class StudyTonight
{  
	public static void main(String args[])
	{  
		double d = 500.0;  
		int i = (int)d;  //typecasting 
		System.out.println("The int value is " +i); 
	}
}


The int value is 500

Also, we can convert a double object to an int by intValue() method of Double class.

Example 2:

Here, the Double object is converted into int by using the intValue() method.

public class StudyTonight
{  
	public static void main(String args[])
	{  
		Double d = new Double(50.78);  
		int i  = d.intValue();  
		System.out.println("The int value is : " +i);   
	}
}


The int value is : 50

Example 3:

Here, the primitive double value is converted into int type value.

public class StudyTonight
{  
	public static void main(String args[])
	{  
		Double myDouble = Double.valueOf(10.0);
		int val = Integer.valueOf(myDouble.intValue());
		System.out.println("The int value is : " +val);   
	}
}


The int value is : 10



About the author:
A Computer Science and Engineering Graduate(2016-2020) from JSSATE Noida. JAVA is Love. Sincerely Followed Sachin Tendulkar as a child, M S Dhoni as a teenager, and Virat Kohli as an adult.