Java – Convert Int to Double
In Java, converting an int to a double is a widening primitive conversion. Because double can represent every 32-bit int value exactly, Java can perform this conversion automatically when an int is assigned to a double.
The simplest and usually preferred approach is direct assignment. If you have an Integer object instead of a primitive int, you can also use Integer.doubleValue(). This tutorial shows both approaches and also covers explicit casting, integer-division pitfalls, and wrapper-type conversion.
1. Convert int to double using widening casting
int is a 32-bit integer type, while double is a 64-bit floating-point type. Java therefore allows an int value to be assigned directly to a double variable without an explicit cast. This automatic conversion is called widening primitive conversion.
The basic syntax is:
int number = 125;
double value = number;
In the following example, we have a variable i which is of int type. We shall assign i to double variable d during its declaration.
Java Program
/**
* Java Program - Convert Int to Double
*/
public class IntToDouble {
public static void main(String[] args) {
int i = 125;
double d = i;
System.out.println(d);
}
}
Output
125.0
The value changes from the integer representation 125 to the double representation 125.0. No explicit cast is required.
2. Convert int to double with an explicit cast
You may also write an explicit cast such as (double) i. For an int-to-double conversion, the cast is optional because Java already performs the widening conversion automatically.
public class IntToDoubleCast {
public static void main(String[] args) {
int i = 250;
double d = (double) i;
System.out.println(d);
}
}
Output
250.0
Use the cast when it makes an expression clearer, but it does not produce a different numeric result from direct assignment in this case.
3. Convert Integer to double using Integer.doubleValue()
If the source value is an Integer object, the doubleValue() method returns the numeric value as a primitive double.
In the following example, we shall take an integer object initialized to a value, and call doubleValue() method on this object.
Java Program
/**
* Java Program - Convert Int to Double
*/
public class IntToDouble {
public static void main(String[] args) {
Integer i = 85;
double d = i.doubleValue();
System.out.println(d);
}
}
Output
85.0
Java can also unbox an Integer and widen the resulting int automatically, so double d = i; is valid when i is a non-null Integer. Calling doubleValue() is useful when you want the conversion to be explicit in code.
Do not cast an Integer object directly to Double
Integer and Double are different wrapper classes. An Integer object is not a Double object, even though both represent numbers. Convert the numeric value instead of trying to cast one wrapper type to the other.
Integer i = 42;
double primitiveValue = i.doubleValue();
Double wrapperValue = i.doubleValue();
If your value is stored in a variable of type Number, doubleValue() is also the general conversion method:
Number number = Integer.valueOf(42);
double d = number.doubleValue();
System.out.println(d);
Output
42.0
Convert before division when you need a decimal result
A common mistake is to convert the result to double only after integer division has already happened. When both operands are integers, Java performs integer division first and discards the fractional part.
int a = 5;
int b = 2;
double wrong = a / b;
double correct = (double) a / b;
System.out.println(wrong);
System.out.println(correct);
Output
2.0
2.5
In double wrong = a / b;, the expression a / b is evaluated as integer division and produces 2; only then is that value widened to 2.0. Casting either operand to double before the division makes the division floating-point division.
Does converting int to double lose precision?
No precision is lost when a Java int is converted to double. Every possible 32-bit int value can be represented exactly by a Java double. This is different from converting some larger integer types, such as certain long values, to double.
Which int-to-double conversion should you use?
- For a primitive
int, use direct assignment such asdouble d = i;. - An explicit
(double)cast is valid but usually unnecessary for simple assignment. - For an
Integeror otherNumberobject, usedoubleValue()when you want an explicit numeric conversion. - If the value is part of integer division, convert an operand to
doublebefore the division. - Do not try to cast an
Integerobject directly to aDoubleobject.
In this Java Tutorial, we learned how to convert an int to double using widening conversion, explicit casting, and Integer.doubleValue(), and how to avoid common mistakes involving wrapper types and integer division.
TutorialKart.com