How to Convert Int to Double in Java

In this tutorial, we will learn how to convert int numeric value to double value, int value to Double object and vice versa in Java easily.

To convert int to double numeric value in Java, we use an assignment operator.

We do not need to do extra to convert int to double. This is because the lower type can be converted to a higher type implicitly.

We also known this conversion as implicit type casting or type promotion.

Let’s create a simple Java program to convert int to double easily.

Program code 1:

// Java Program to demonstrate the conversion of int into double implicitly.
package javaConversion;
public class IntToDoubleConversion {
public static void main(String[] args) 
{  
 int num = 900;  
 double d = num;  
 System.out.println("Double numeric value after conversion: " +d);
 }
}
Output:
     Double numeric value after conversion: 900.0

Converting Int to Double object in Java using Double.valueOf() method


We can convert int numeric value to Double object by creating an object of Double wrapper class and then call its valueOf() method.

Let’s make a simple Java program for converting int to Double object using valueOf() method of Double class.

Program code 2:

// Java Program to demonstrate the conversion of int into double object.
package javaConversion;
public class IntToDoubleConversion {
public static void main(String[] args) 
{  
 int num = 900;  
 Double d1 = new Double(num); // first way  
 Double d2 = Double.valueOf(num); // second way  
 System.out.println(d1);  
 System.out.println(d2); 
 }
}
Output:
      900.0
      900.0

In the above program, the valueOf() method of Double wrapper class returns a Double instance representing the specified double value.

Converting Double to Int in Java using Typecasting


To convert double to int numeric value in Java, we use typecasting. To convert the higher data type to lower, we perform typecasting in the program through a typecast operator (datatype).

Let’s make a Java program for converting double primitive type into int value using typecasting.

Program code 3:

// Java Program to demonstrate the conversion of double into int value.
package javaConversion;
public class DoubleToIntConversion {
public static void main(String[] args) 
{  
 double d = 99.99;  
 int i = (int)d; // typecasting. 
 System.out.println(i);  
 }
}
Output:
      99

Double object to int Conversion


For converting Double object to int value in Java, we use intValue() method of Double class. Let’s take a simple program for converting Double object to int in Java.

Program code 4:

// Java Program to demonstrate the conversion of Double object into int value using intValue() method.
package javaConversion;
public class DoubleToIntConversion {
public static void main(String[] args) 
{  
 Double d = new Double(999.999);  
 int i = d.intValue();  
 System.out.println(i);  
 }
}
Output:
      999

In this example program, we have used intValue() method of Double class for converting Double object to int value. The intValue() method returns the value of this Double as an int after a narrowing primitive conversion.


In this tutorial, you learned how to convert a primitive type int value to double and vice versa in Java easily. Hope that you will have understood and practiced all programs based on the conversion of int to double in Java.
Thanks for reading!!!
Next ⇒ Convert Char to Int in Java⇐ Prev Next ⇒