EN
Java - convert int to double
6
points
Short solution:
int intNumber = 200;
double doubleNumber = (double) intNumber; // 200
Another simple solutions:
// solution 2 (implicit casting)
int intNumber2 = 200;
double doubleNumber2 = intNumber2; // 200
// solution 3
int intNumber3 = 200;
double doubleNumber3 = 1.0d * intNumber3; // 200 // or: 1.0 * intNumber3
// solution 4
int intNumber4 = 200;
Double doubleNumber4 = new Double(intNumber4); // 200
// solution 5
int intNumber5 = 200;
Double doubleNumber5 = Double.valueOf(intNumber5); // 200
// solution 6
Integer number6 = 200;
double doubleNumber6 = number6.doubleValue(); // 200
In java we can convert int to double in following ways.
1. Using explicit double casting
public class Example1 {
public static void main(String[] args) {
int intNumber = 200;
double doubleNumber = (double) intNumber;
doubleNumber += 0.5;
System.out.println(doubleNumber); // 200.5
}
}
Output:
200.5
2. Using implicit double casting
public class Example2 {
public static void main(String[] args) {
int intNumber = 200;
double doubleNumber = intNumber;
doubleNumber += 0.5;
System.out.println(doubleNumber); // 200.5
}
}
Output:
200.5
3. Using multiplication by double number
public class Example3 {
public static void main(String[] args) {
int intNumber = 200;
double doubleNumber = 1.0d * intNumber; // or: 1.0 * intNumber
doubleNumber += 0.5;
System.out.println(doubleNumber); // 200.5
}
}
Output:
200.5
4. Convert int to Double using Double constructor
public class Example4 {
public static void main(String[] args) {
int intNumber = 200;
Double doubleNumber = new Double(intNumber);
doubleNumber += 0.5;
System.out.println(doubleNumber); // 200.5
}
}
Output:
200.5
5. Convert int to Double using Double.valueOf()
public class Example5 {
public static void main(String[] args) {
int intNumber = 200;
Double doubleNumber = Double.valueOf(intNumber);
doubleNumber += 0.5;
System.out.println(doubleNumber); // 200.5
}
}
Output:
200.5
6. Convert Integer to Double using Integer doubleValue()
public class Example6 {
public static void main(String[] args) {
Integer number = 200;
double doubleNumber = number.doubleValue();
doubleNumber += 0.5;
System.out.println(doubleNumber); // 200.5
}
}
Output:
200.5