Examples
- How to Convert Char to String in Java? Character to String
- How to Convert Char to Int in Java? Character to Integer Conversion
- How to Convert Int to Char in Java? Integer to Character
- How to Convert Long to Int in Java? Long to Integer
- How to Convert Int to Long in Java? Integer to Long Program
- How to Convert Boolean to String in Java? (Parse Bool to String)
- How to Convert String to Boolean in Java? 3 Ways With Program
- How to Convert String to Int in Java? String to Integer Program
- How to Convert Int to String in Java? Integer to String Program
- How to Convert Int to Double in Java? (Integer to Double)
- How to Convert Double to Int in Java? 4 Ways
- How to Convert String to Double in Java? Programs
- How to Convert Double to String in Java? Programs
How to Convert Double to Int in Java? 4 Ways
While working with Java, it's not uncommon to encounter situations where you need to work with numerical data in different formats. One common scenario is the need to convert a double (a floating-point number) into an int (an integer). Whether you're dealing with financial calculations, user input, or any other mathematical operation, understanding how to convert double to int in Java is a fundamental skill.
This tutorial will walk you through the step-by-step process of converting double to int in Java, offering both a clear explanation of the underlying concepts and practical examples to reinforce your understanding.
By the end of this tutorial, you'll have a solid grasp of the different methods available for converting double to integer in Java, along with insights into when and why you might choose one approach over another.
Concepts to Learn for This Program:
Double to Int in Java Using Type Casting
Here's a Java program to convert double to int using type casting, along with an explanation and the output:
Code
public class DoubleToIntConversion {
public static void main(String[] args) {
// Define a double value
double doubleValue = 123.456;
// Convert the double to an int using type casting
int intValue = (int) doubleValue;
// Output the results
System.out.println("Original Double Value: " + doubleValue);
System.out.println("Converted Int Value: " + intValue);
}
}
Output
Original Double Value: 123.456
Converted Int Value: 123
Explanation
-
We begin by defining a doubleValue variable and assigning it the value 123.456. This is the double value that we want to convert to an int.
-
To convert the doubleValue to an int, we use Java type casting. The (int) before doubleValue indicates that we are explicitly converting the double to an int.
-
The converted int value is stored in the intValue variable.
-
Finally, we output both the original double value and the converted int value using System.out.println().
In this program, the double value 123.456 is successfully converted to an int, and the decimal part is truncated, resulting in the int value 123. This show how type casting can be used for a simple conversion from double to int in Java.
Convert Double to Int in Java Using Double.intValue()
Here's a program on how to convert double to int in Java using the Double.intValue() method:
Code
public class DoubleToIntConversion {
public static void main(String[] args) {
// Define a double value
double doubleValue = 123.456;
// Convert the double to an int using Double.intValue() method
int intValue = Double.valueOf(doubleValue).intValue();
// Output the results
System.out.println("Original Double Value: " + doubleValue);
System.out.println("Converted Int Value: " + intValue);
}
}
Output
Original Double Value: 123.456
Converted Int Value: 123
Explanation
-
We start by defining a doubleValue variable and assigning it the value 123.456. This is the double value that we want to convert to an int.
-
To convert the doubleValue to an int, we use the Double.valueOf(doubleValue).intValue() method. Here, we first create a Double object from the doubleValue, and then we use the intValue() method to extract the int value.
-
The converted int value is stored in the intValue variable.
-
Finally, we output both the original double value and the converted int value using System.out.println().
Related Java Programs:
Convert Double to Int in Java Using Math.round()
Here's a Java program to convert double to int using the Math.round() method:
Code
public class DoubleToIntConversion {
public static void main(String[] args) {
// Define a double value
double doubleValue = 123.456;
// Convert the double to an int using Math.round() method
int intValue = (int) Math.round(doubleValue);
// Output the results
System.out.println("Original Double Value: " + doubleValue);
System.out.println("Converted Int Value: " + intValue);
}
}
Output
Original Double Value: 123.456
Converted Int Value: 123
Explanation
-
We start by defining a doubleValue variable and assigning it the value 123.456. This is the double value that we want to convert to an int.
-
To convert the doubleValue to an int using Math.round(), we first call Math.round(doubleValue) to round the double value to the nearest integer. This method returns a long, so we cast it to an int using (int).
-
The converted int value is stored in the intValue variable.
-
Finally, we output both the original double value and the converted int value using System.out.println().
Convert Double to Integer in Java Using Math.floor()
Following is the program on how to convert double to int using the Math.floor() method:
Code
public class DoubleToIntConversion {
public static void main(String[] args) {
// Define a double value
double doubleValue = 123.456;
// Convert the double to an int using Math.floor() method
int intValue = (int) Math.floor(doubleValue);
// Output the results
System.out.println("Original Double Value: " + doubleValue);
System.out.println("Converted Int Value: " + intValue);
}
}
Output
Original Double Value: 123.456
Converted Int Value: 123
Explanation
-
We start by defining a doubleValue variable and assigning it the value 123.456. This is the double value that we want to convert to an int.
-
To convert the doubleValue to an int using Math.floor(), we first call Math.floor(doubleValue) to round down the double value to the nearest integer that is less than or equal to the original value. This method returns a double, so we cast it to an int using (int).
-
The converted int value is stored in the intValue variable.
-
Finally, we output both the original double value and the converted int value using System.out.println().
Learn Next: