How to convert Int to Double in Java

The primitive data type in Java is represented by the int keyword. It’s where variables are declared. It can also be used with methods that return values of the integer type. A 32-bit signed two’s complement integer can be stored in it. The concept of an integer is familiar to most of us from our elementary school days. An integer is a number that doesn’t have a fractional component. In other words, an integer is comparable to a whole number, except it also has negative values. Thus, non-decimal numbers may both be positive and negative. It refers to a memory location (or locations) that is used to store non-decimal or non-fractional positive or negative numbers. Today let us know How to Convert Int to Double in Java .   

The keyword ‘int’ signifies an integer type, which describes the memory needed to store the data. Additionally, this type has limitations on how it can be used. For example, fractions cannot be stored in integers. Let us know about the Double data type before we move on to How to Convert Int to Double in Java.

The double data type, also known as double, is a data type in the JAVA programming language that aids in the storage of high-precision floating-point numbers or data in computer memory. This data type is also known as double because it can hold twice the amount of information and data as a float. As a result, a double is made up of 8 bytes of data, which is roughly 64 bits in size. The exponent of the double is represented by 11 bits, the sign is represented by 1 bit, and the mantissa of the double is represented by the remaining 52 bits. The range of the double data type is 1.7E-308 to 1.7E+308.

 

What’s The Approach?

We can do this program in 3 ways:-

  • Import the “java.util” package

 

  • Create a Scanner class

 

  • Accept the input from the user

 

  • Convert the int to double using the implicit type casting as a lower type can be converted to a higher type implicitly.

 

  • We can also convert int to double by using the double class

 

  • The Int can be converted to double even by using the Double.valueOf() method.

 

  • Print the double value.

 

Also Read: How to convert Long to Int in Java.

 

Java Program To Convert Int to Double in Java: –

 

1. Method 1:

/*
 * TechDecode Tutorials
 * 
 * How to Covert Int to Double
 * 
 */

import java.util.*;
class Int_to_Double
{
    public static void main(String args[])
    {
        //Creating Scanner Class
        Scanner sc=new Scanner(System.in);
        //Accpeting users input
        System.out.print("Enter the Int value: ");
        int num=sc.nextInt();
        //Converting to double
        double d = num;
        System.out.println("Int Converted to Double is : "+d );
        
    }
}


 

2. Method 2:

/*
 * TechDecode Tutorials
 * 
 * How to Covert Int to Double
 * 
 */

import java.util.*;
class Int_to_Double
{
    public static void main(String args[])
    {
        //Creating Scanner Class
        Scanner sc=new Scanner(System.in);
        //Accpeting users input
        System.out.print("Enter the Int value: ");
        int num=sc.nextInt();
        //Converting to double
        double d = new Double(num);
        System.out.println("Int Converted to Double is : "+d );
        
    }
}


 

3. Method 3:

/*
 * TechDecode Tutorials
 * 
 * How to Covert Int to Double
 * 
 */

import java.util.*;
class Int_to_Double
{
    public static void main(String args[])
    {
        //Creating Scanner Class
        Scanner sc=new Scanner(System.in);
        //Accpeting users input
        System.out.print("Enter the Int value: ");
        int num=sc.nextInt();
        //Converting to double
        double d = Double.valueOf(num);
        System.out.println("Int Converted to Double is : "+d );
        
    }
}

 

Output:

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *