Java Program to Convert int to double - The Coding Shala
Hey there, welcome back to another post. In this post, we are going to learn how to convert int to double in Java.
Java Program to Convert int to double
As we know, double is a larger data type than int so the Java compiler automatically converts int to double when we assign an int to double data type. The most common ways are below to convert int to double:
- Implicit type conversion.
- By using the Double Wrapper class's valueOf() method.
Now let's see the above methods in detail with examples in Java.
Java Example to Convert int to double by Implicit Conversion
The Java compiler will automatically convert smaller data types to larger data types.
Java Program:
/** * https://www.thecodingshala.com/ */ public class Main { public static void main(String[] args) { int num = 50; // implicit type conversion double dNum = num; System.out.println("int is: " + num); System.out.println("Converted double is: " + dNum); } }
Output:
int is: 50 Converted double is: 50.0
Java Example to Convert int to double by Using Double.valueOf() method
We can use the Double Wrapper class's valueOf() method to convert int to double data type.
Java Program:
/** * https://www.thecodingshala.com/ */ public class Main { public static void main(String[] args) { int num = 50; // by using Double.valueOf() double dNum = Double.valueOf(num); System.out.println("int is: " + num); System.out.println("Converted double is: " + dNum); } }
Output:
int is: 50 Converted double is: 50.0
Other Posts You May Like
- Java Program to Convert double to int
- Java Program to Convert String to int
- Java Program to Convert int to String
- Java Program to Convert char to int
- Java Program to Convert int to char
Please leave a comment below if you like this post or found some errors, it will help me to improve my content.
Comments
Post a Comment