Double is a primitive data type in Java that is used to represent double-precision floating point numbers. It uses the IEEE 754 standard for representing floating point numbers and has a default value of 0.0d. The double
data type is the default data type for decimal numbers in Java and has a default size of 8 bytes.
Example of Double
double d1 = 19.5
The Integer data type, also referred to as the int data type, is a 32-bit signed and consists of a complement of two integers. It has a range of values from -2^31 to 2^31 -1 (inclusive). The minimum value of the Integer data type is “-2,147,483,648” and the maximum value is “2,147,483,647”. 0 is the default value of int data type. Int data types are typically used as default data types for integral values unless there is an issue with memory.
Example of Integer
int a = 10
Methods for Double-to-Integer Conversion
You can use any of the following approaches for converting Double to int in Java
- Using TypeCasting
- Using Double.intValue() method
- Using Math.round() method
- Using Ceil() method
- Using floor() method
1. Using TypeCasting
Typecasting is a process of converting one data type to another. It is primarily utilized by Java developers to test the data variable. You can also use it for converting double data type variables to int.
Syntax
int i = (double) d;
Example:
// Java Program to convert // Double to Integer // Using TypeCasting // Driver Class public class TypecastingOverflow {
// main function
public static void main(String[] args)
{
double doubleVal = 327.33 ;
int smallInt = 0 ;
try {
// Using typecasting
smallInt = ( int )doubleVal;
}
catch (ArithmeticException e) {
System.out.println( "Overflow error: " + e);
smallInt = 0 ;
}
// Printing the Result
System.out.println( "Double: " + doubleVal);
System.out.println( "Int: " + smallInt);
}
} |
Double: 327.33 Int: 327
2.Using Double.intValue()
A second method, Double.intValue(), is used to transform a number from a double data type into an integer. This method is implemented by the Java “Double” Wrapper class, and is analogous to typecasting. The “intValue()” method converts the value of a double into the closest integer value, yielding an integer.
Syntax
int i = new Double(d).intValue();
Example:
// Java Program to convert // Double to Integer // Using Double.intValue() // Driver Class public class DoubleToIntOverflow {
// main function
public static void main(String[] args)
{
Double largeDouble = Double.valueOf( 1.2345 );
int smallInt = 0 ;
try {
// Using Double.intValue()
smallInt = largeDouble.intValue();
}
catch (ArithmeticException e) {
System.out.println( "Overflow error: " + e);
smallInt = 0 ;
}
// Printing result
System.out.println( "Large Double: " + largeDouble);
System.out.println( "Small Int: " + smallInt);
}
} |
Large Double: 1.2345 Small Int: 1
3. Using Math.round()
Math.round() method is used to round a double value to the nearest integer. This method is part of the Java “Math” class. Math.round() takes a double value and rounds it to the nearest long value. This is done by incrementing the value by 0.5 and rounding the long value to the nearest integer. The long value is then converted to an integer using typecasting.
Syntax
int i = (int)Math.round(d);
Example:
// Java Program to convert // Double to Integer // Using Math.round() // Driver Class public class MathRoundOverflow {
// main function
public static void main(String[] args)
{
double largeDouble = 21474.76 ;
int roundedInt;
// Using Math.round()
roundedInt = ( int )Math.round(largeDouble);
// Printing the result
System.out.println( "Large Double: " + largeDouble);
System.out.println( "Rounded Int: " + roundedInt);
}
} |
Large Double: 21474.76 Rounded Int: 21475
Note:
- Math.round() will follow all mathematical rounding rules
- If you want just to remove the decimal part, add a separate condition after rounding.
4. Using Ceil()
In the ceil() method, the double value is rounded up and returned. The rounded value is equal to a mathematical integer. For example, the double value 3.24 is rounded up to 4.0, which is an integer of 4.
Syntax
int i = Math.ceil(value)
Example:
// Java Program to convert // Double to Integer // Using Ceil() // Driver Class public class MathCeilOverflow {
// main function
public static void main(String[] args) {
double largeDouble = 214.87 ;
double ceiledDouble;
// Using Ceil()
ceiledDouble = Math.ceil(largeDouble);
// Printing the Result
System.out.println( "Large Double: " + largeDouble);
System.out.println( "Ceiled Double: " + ceiledDouble);
}
} |
Large Double: 214.87 Ceiled Double: 215.0
Note:
- Math.ceil() will follow all mathematical ceil rules
- If you want just to remove the decimal part, add a separate condition after rounding.
5. Using floor()
The Java Math.floor() function is a mathematical function that returns a double value equal to or less than the parameter passed to the function. The function returns the nearest mathematical integer. The floor() method takes the specified double value and rounds it down. The rounded value is a mathematical integer. For example, the value of 3.8 is rounded down to 3.0, which is an integer of 3.
Syntax
int i = Math.floor(value)
Example:
// // Java Program to convert // Double to Integer // Using floor() // Driver Class public class MathFloorOverflow {
// main function
public static void main(String[] args) {
double largeDouble = 214.87 ;
double flooredDouble;
// Using floor()
flooredDouble = Math.floor(largeDouble);
// Printing the Result
System.out.println( "Large Double: " + largeDouble);
System.out.println( "Floored Double: " + flooredDouble);
}
} |
Large Double: 214.87 Floored Double: 214.0
Note:
- Math.floor() will follow all mathematical floor rules
- If you want just to remove the decimal part, add a separate condition after rounding.