Java Program to Convert Int to Double

This article explains how to write a Java program to convert int or integer to a double data type. In Java, we can use the assignment operator (=) to convert lower data type (int) to higher data type (double). This assignment operator implicitly converts the integer to double, also called implicit typecasting.

In the following Java example, we declared an integer variable and assigned a value. Next, we assigned it to a double variable.

public class IntToDouble {

	public static void main(String[] args) {
		
		int i = 240;
		
		double dd1 = i;
		
		System.out.println("Original Integer Value  = " + i);
		System.out.println("Int Converted to Double = " + dd1);
	}
}
Original Integer Value  = 240
Int Converted to Double = 240.0

Java Program to Convert Int to Double example 2

There are few other ways to convert int to Double in Java. The first option is to initiate integer as double, and the other option is the Double.valueOf() function.

import java.util.Scanner;

public class IntToDouble {
	private static Scanner sc;
	
	public static void main(String[] args) {
		int i;
		sc= new Scanner(System.in);

		System.out.println("\n Please Enter any Integer Value :  ");
		i = sc.nextInt();
		
		double d1 = i;
		
		double d2 = new Double(i);
		
		double d3 = Double.valueOf(i);
		
		System.out.println("The first way to convert int to Double  = " + d1);
		System.out.println("The second way to convert int to Double = " + d2);
		System.out.println("The third way to convert int to Double  = " + d3);
	}
}
Java Program to Convert Int to Double 2