LAST UPDATED: DECEMBER 1, 2020
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