Java convert from double to int: In the previous article we have discussed Java Program to Convert double to String
In this article we will see how to convert a double to integer.
Program to Convert double to int
Before going into the actual program, let’s see some examples of both the types.
Example-1: double type double a = 3.123456789; double b = 3.5E02;
Example-2: int type int a = 23; int b=5;
Let’s see different ways to do it.
Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language
Method 1 : Java Program to Convert double to int Using typecasting
Double type variable can be converted to integer by using typecasting let’s see how it will work.
Here this method is also called narrowing typecasting because in this the higher data type double is converted into the lower data type int.
Approach :
- Take a double value and store it in a double variable
input1
. - Typecast the variable with
int
and store it in a variableoutput
. - Display the result .
Program:
import java.util.Scanner; public class Main { public static void main(String[] args) { // creating Scanner object Scanner sc = new Scanner(System.in); // input a double value through scanner class System.out.print("Enter a Double : "); double input1=sc.nextDouble(); // converting to intiger int output= (int)input1 ; System.out.println("Converted Integer value is : " + output); } }
Output : Enter a Double : 6.33 Converted Integer value is : 6
Method 2 : Java Program to Convert double to int Using Math.round() method
Double type variable can be converted to integer by using Math.round()
, let’s see how it actually works.
Here, the Math.round()
method will convert the Double type variable into int
type & returns it.
Approach :
- Take a Double type value and store it in a
double
variableinput1
- Then pass that input1 variable as parameter to
Math.round()
method which will convert thelong
toint
value and return it . - Store that int value in a variable
output
. - Display the result .
Program:
import java.util.Scanner; public class Main { public static void main(String[] args) { // creating scanner object Scanner sc = new Scanner(System.in); // input a double value through scanner class System.out.print("Enter a Double value : "); double input1=sc.nextDouble(); // converting to Integer int output= (int)Math.round(input1) ; System.out.println("Converted Integer value is : " + output); } }
Output : Enter a Double value : 6.33 Converted Integer value is : 6
Method 3 : Java Program to Convert double to int By Converting the object of the Double class to int
Double type variable can be converted to integer by Converting the object of the Double class
to int
, let’s see how it actually works.
By using intValue()
method we can convert the object of wrapper class Double into an int.
Approach :
- Take a double value and store it in a double object
ob
. - Then pass that object as
ob.intValue()
which will convert the Double to int value and return it . - Store that int value in a variable
output
. - Display the result .
Program:
import java.util.Scanner; public class Main { public static void main(String[] args) { // creating scanner object Scanner sc = new Scanner(System.in); //input a double value through scanner class System.out.print("Enter a Double value : "); Double ob=sc.nextDouble(); // converting to integer int output= ob.intValue(); System.out.println("Converted Integer value is : " + output); } }
Output : Enter a Double : 6.33 Converted Integer value is : 6
Practice Java programming from home without using any fancy software just by tapping on this
Simple Java Programs for Beginners tutorial.
Related Java Program: