5 Ways to Convert Double to Integer in Java
Last Updated :
02 Jan, 2024
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
public class TypecastingOverflow {
public static void main(String[] args)
{
double doubleVal = 327.33 ;
int smallInt = 0 ;
try {
smallInt = ( int )doubleVal;
}
catch (ArithmeticException e) {
System.out.println( "Overflow error: " + e);
smallInt = 0 ;
}
System.out.println( "Double: " + doubleVal);
System.out.println( "Int: " + smallInt);
}
}
|
Output
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
public class DoubleToIntOverflow {
public static void main(String[] args)
{
Double largeDouble = Double.valueOf( 1.2345 );
int smallInt = 0 ;
try {
smallInt = largeDouble.intValue();
}
catch (ArithmeticException e) {
System.out.println( "Overflow error: " + e);
smallInt = 0 ;
}
System.out.println( "Large Double: " + largeDouble);
System.out.println( "Small Int: " + smallInt);
}
}
|
Output
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
public class MathRoundOverflow {
public static void main(String[] args)
{
double largeDouble = 21474.76 ;
int roundedInt;
roundedInt = ( int )Math.round(largeDouble);
System.out.println( "Large Double: " + largeDouble);
System.out.println( "Rounded Int: " + roundedInt);
}
}
|
Output
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
public class MathCeilOverflow {
public static void main(String[] args) {
double largeDouble = 214.87 ;
double ceiledDouble;
ceiledDouble = Math.ceil(largeDouble);
System.out.println( "Large Double: " + largeDouble);
System.out.println( "Ceiled Double: " + ceiledDouble);
}
}
|
Output
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
public class MathFloorOverflow {
public static void main(String[] args) {
double largeDouble = 214.87 ;
double flooredDouble;
flooredDouble = Math.floor(largeDouble);
System.out.println( "Large Double: " + largeDouble);
System.out.println( "Floored Double: " + flooredDouble);
}
}
|
Output
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.
Share your thoughts in the comments
Please Login to comment...