Java Program to convert double type variables to int
What are type conversion?
Type conversion, also known as type casting, in Java is the process of converting one data type to another data type. There are two types of type conversion in Java: Implicit type conversion and Explicit type conversion.
In this Article, we will write a program to convert char type variables to int.
double to int conversion :
Converting a double value to an int means converting a double-precision floating-point number to a 32-bit integer. This conversion can be done using a cast operator, which discards the fractional part of the double and rounds down to the nearest integer.
1. Program to Convert double to int using Typecasting :
A double variable can be assigned to an int variable by converting or typecasting it into the int variable. This is known as an variable typecasting.
Example:
// Importing all the library functions import java.util.*; public class Main{ public static void main(String[] args){ // Initializing the double variable double myDouble = 4.23; // typecasting double variable into int int myInt = (int) myDouble; // Printing the variables System.out.println("Double value: " + myDouble); System.out.println("Int value: " + myInt); } }
Output: Double value: 4.23 Int value: 4
Explanation:
In this example, the value of myDouble is 4.23, and after the conversion, the value of myInt is 4. The fractional part of the double value is truncated, not rounded, and the result is stored in the integer variable myInt.
2. Program to Convert double to int using floor method :
The Math.floor is a static method in the java.lang.Math class that returns the largest integer that is less than or equal to the argument passed to it. It takes a double value as an argument and returns the corresponding double value rounded down to the nearest integer.
Example:
// Importing all the library functions import java.util.*; import java.lang.Math; public class Main{ public static void main(String[] args){ // Initializing double variable double myDouble = 4.17; // Converting int variable to double using math.floor method int myInt = (int) Math.floor(myDouble); // Printing the variables System.out.println("Double value: " + myDouble); System.out.println("Int value: " + myInt); } }
Output: Double value: 4.17 Int value: 4
Explanation:
In the above example, the Math.floor method is used to round down the double value to the nearest integer. The result is stored in the integer variable myInt.
3. Program to Convert double to int using Math.round() method :
Math.round is a static method in the java.lang.Math class that returns the closest integer to the argument passed to it. It takes a floating-point number as an argument and returns the corresponding rounded integer value.
Example :
// Importing all the library functions import java.util.*; import java.lang.Math; public class Main{ public static void main(String[] args){ // Initializing the double variable double myDouble = 4.27; // Converting the double variable to int using Math.round() method int myInt = (int) Math.round(myDouble); // Printing the variable System.out.println("Double value: " + myDouble); System.out.println("Int value: " + myInt); } }
Output: Double value: 4.27 Int value: 4
Explanation:
In the above example, the value of myDouble is 4.27, and after the conversion, the value of myInt is 4. The Math.round method rounds the double value to the nearest integer, so the result is stored in the integer variable myInt.
Note : The Math.round is commonly used to convert floating-point numbers to integers when the result should be rounded to the nearest integer.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment