In this article we will learn how to convert double to int in java. This is very easy in java. A lot of viewers already know this. But I will tell you how you can do it easily. I will use two methods to convert double to int in java.
- Math.round() method
- Double.intValue()
How to Convert Double to Int in Java:
Math.round() .
First of all lets talk about the predefined method Math.round() This method is used to convert the Double value to the nearest integer.
Example:
import java.util.Scanner; public class doubletoint { public static void main(String [] Args){ Scanner sn = new Scanner (System.in); System.out.print("Enter a Number with decimal digits "); double Num = sn.nextDouble(); int num2 = (int)Math.round(Num); System.out.println("The decimal number with decimal digits is convereted to integer - " +num2); } }
Double.intValue()
The second method id Double.intValue(). In this method we will create the object of double. Then the object of double will be written with .intValue(). Here is the code.
Example:
import java.util.Scanner; public class doubletoint { public static void main(String [] Args){ Scanner sn = new Scanner (System.in); System.out.print("Enter a Number with decimal digits "); double Num = sn.nextDouble(); Double Num2 = new Double(Num); int Num3 = Num2.intValue(); System.out.println("The decimal number with decimal digits greater than 5 is convereted to integer - " +Num3); } }
Conclusion:
In this article we have studied the two most easiest method to convert double to int in java. I hope you guys understand it. Please share it with your friends also Thanks.