Convert int to double in Java
Tags: double int Integer Convert
In this Java core tutorial, we learn how to convert int to double value in Java via different solutions.
Assign int variable to a double variable
ConvertIntToDoubleExample1.java
public class ConvertIntToDoubleExample1 {
public static void main(String[] args) {
int value1 = 2021;
double value2 = value1;
System.out.println("int value: " + value1);
System.out.println("double value: " + value2);
}
}
int value: 2021
double value: 2021.0
How to use Double.valueOf() method to instantiate new Double object
ConvertIntToDoubleExample2.java
public class ConvertIntToDoubleExample2 {
public static void main(String[] args) {
int value1 = 1999;
Double value2 = Double.valueOf(value1);
System.out.println("int value: " + value1);
System.out.println("double value: " + value2);
}
}
int value: 1999
double value: 1999.0
How to use Integer.doubleValue() method to get double value of an Integer
ConvertIntToDoubleExample3.java
public class ConvertIntToDoubleExample3 {
public static void main(String[] args) {
int value1 = 12;
Integer integerValue = value1;
double value2 = integerValue.doubleValue();
System.out.println("int value: " + value1);
System.out.println("double value: " + value2);
}
}
int value: 12
double value: 12.0
Happy Coding 😊