# C++ Program to convert int Variables into double

Int type variables in C++ are used to contain positive or negarive integer values, but this type is unable to contain fractional values. For that, there are float and double values. Double datatype is specifically designed to hold fractional values upto seven digits after the decimal point. Conversion between integer and double variables can be automatically handled by the compiler, known as ‘implicit’ conversion or can be explicitly triggered by the programmer to the compiler. We discuss the different ways of conversion in the following sections.

## Implicit Conversion

Implicit type conversions are done automatically by the compiler. For this to occur, we take two variables; one of integer type and the another one of float type. Then we just assign the integer value or variable to the float variable and everything else will be handled by the compiler.

### Algorithm

• Take an integer value as the input.
• Assign the value to a double variable.
• Display the output.

### Syntax

int input = <integer value>;
double output = input;


### Example

#include <iostream>
using namespace std;

double solve(int value) {
double opVal = value;
return opVal;
}

int main()
{
int ip = 25;
double op = solve(ip);
cout<< "The input value is: " << ip << endl;
cout<< "The output value is: " << op << endl;
return 0;
}


### Output

The input value is: 25
The output value is: 25


As we can see the conversion process is pretty straightforward. We do not need to perform any kind of special operations; we just assign the input variable to the output variable.

## Explicit conversion

Explicit conversion happens when the programmer specifically instructs the compiler to convert a data type to another. This can be done using two ways, one is by mentioning the data type during assignment, and the another one is using static_cast. We describe the first method before.

### Algorithm

• Take an integer value as the input;
• Assign the value to a double variable using explicit casting to boolean.
• Display the output.

## Mentioning datatype during assignment

This can also be done in two different ways. One is the C-styled version and another one is the function-styled casting.

## C-Styled version

We mention the resultant datatype before the source variable or the value enclosed within brackets.

### Syntax

int input = <integer value>;
double output = (double) input;


### Example

#include <iostream>
using namespace std;
double solve(int value) {
double opVal = (double) value;
return opVal;
}

int main()
{
int ip = 35;
double op = solve(ip);
cout<< "The input value is: " << ip << endl;
cout<< "The output value is: " << op << endl;
return 0;
}


### Output

The input value is: 35
The output value is: 35


## Function-styled casting

We mention the resultant datatype and enclose the source value within brackets as we do while passing an argument to a function.

### Syntax

int input = <integer value>;
double output = double(input);


### Example

#include <iostream>
using namespace std;
double solve(int value) {
double opVal = double(value);
return opVal;
}

int main()
{
int ip = 45;
double op = solve(ip);
cout<< "The input value is: " << ip << endl;
cout<< "The output value is: " << op << endl;
return 0;
}


### Output

The input value is: 45
The output value is: 45


## Using static_cast

### Syntax

int input = <integer value>;
double output = static_cast<double>(input);


### Example

#include <iostream>
using namespace std;

double solve(int value) {
double opVal = static_cast<double>(value);
return opVal;
}

int main()
{
int ip = 55;
double op = solve(ip);
cout<< "The input value is: " << ip << endl;
cout<< "The output value is: " << op << endl;
return 0;
}


### Output

The input value is: 55
The output value is: 55


From the last three examples, we can see that the explicit conversion process is almost similar whether we use static_cast, C-styled casting, or the function styled casting. In all three cases, we have to mention the resultant data type before the assignment.

## Conclusion

The several methods for converting an integer to a double value were covered. The programmer must determine which conversion method is most suited for the particular case because different conversion scenarios call for different conversion methods. However, because implicit conversions are carried out automatically, the programmer should not be concerned with carrying out sophisticated tactics.