CodeGym /Java Blog /Java Numbers /How to convert int to double in Java
Author
Pavlo Plynko
Java Developer at CodeGym

How to convert int to double in Java

Published in the Java Numbers group

Briefly about int and double types in Java

int is a primitive Java type for integer numbers (such as -25, 0, 1828182845). This type uses 32 bits to store a variable value. The range of int numbers is from -231 to 231 - 1 or, which is the same, from -2147483648 to 2147483647. Double type in Java represents floating-point numbers, allocates 64 bits in memory and the range of the type is -1.7*10308 to 1.7*10308. If you represent the range of int in the same form, it will be -2*109 до +2*109. I think it is obvious that any number of type int will fit into the memory allocated for a number of double primitive type. In addition, any integer can be represented as a fractional number with a zero fractional part. From the math point of view, there is no doubt that: 5 = 5.0 или -57.0 = -57.

Converting int to double

From a Java perspective, double and int types are also compatible. Since converting int to double is about casting bigger to smaller, this kind of conversion calls implicit type casing or widening. You can convert int to double in Java automatically, by assigning the int value to a double variable. Let’s have a code example of typecasting:

public class intToDouble {
   public static void main(String[] args) {
       int myInt1 = 10;
       int myInt2 = 2147483647;
       double myDouble1, myDouble2;
       System.out.println("my integers are: " + myInt1 + ", " + myInt2);
       myDouble1 = myInt1;
       myDouble2 = myInt2;
       System.out.println("after typecasting/widening to double: " + myDouble1 + ", " + myDouble2);
   }
}
Here is the output:
my integers are: 10, 2147483647 after typecasting/widening to double: 10.0, 2.147483647E9
Notes: E9 here means 109, it is called scientific notation. Note also that double numbers are usually written with a period separating the fractional part. if you declare a variable of double and put a value in it, it is not necessary to do this, but in the output the double number will always have a fractional part, even if it is zero.

Converting int to double using neutral numerical operation

Moreover, all numeric operations on variables of different types in Java lead to type widening. That is, the result of the operation will be of a wider type. Therefore, to convert from int to double, you can use the “neutral” operation. For example, multiply an int by 1.0 (a double number) or add 0.0 to an int. Here is an example of such typecasting:

public class intToDouble {
   public static void main(String[] args) {
       double a = 1;  //you can also write 1.0 here. If you print it out it will be 1.0
       int b = 5, x = 7;
       System.out.println(x + 0.0);
       System.out.println(a*b);
   }
}
The output is:
7.0 5.0
By the way, you can convert to double not only int, but all numeric primitive types. Here is the order of possible conversion from smallest to largest:
Byte -> Short -> Char -> Int -> Long -> Float -> Double
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION