In C, variables of type Int are used to store positive or negative integer values, but this type cannot contain decimal values. For this, there are float and double values. The Double data type is specifically designed to retain decimal values to seven decimal places. Conversions between integer and double variables can be handled automatically by the compiler, called "implicit" conversions, or they can be explicitly triggered to the compiler by the programmer. We will discuss different conversion methods in the following chapters.
Implicit type conversion is automatically completed by the compiler. To achieve this, we need two variables; one of type integer and the other of type floating point. Then we just assign the integer value or variable to the floating point variable and everything else will be taken care of by the compiler.
int input = <integer value>; double output = input;
#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; }
The input value is: 25 The output value is: 25
As we can see, the conversion process is very simple. We don't need to do anything special, just assign the input variable to the output variable.
Explicit conversion occurs when the programmer explicitly instructs the compiler to convert one data type to another. This can be achieved in two ways, one is to specify the data type during assignment, the other is to use static_cast. We describe the first method first.
This can also be done in two different ways. One is a C-style version and the other is a functional-style conversion.
The Chinese translation ofWe mentioned the result data type before the source variable or the value contained in parentheses.
int input = <integer value>; double output = (double) input;
#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; }
The input value is: 35 The output value is: 35
We mentioned the result data type and enclose the source value in parentheses when passing parameters to the function.
int input = <integer value>; double output = double(input);
#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; }
The input value is: 45 The output value is: 45
int input = <integer value>; double output = static_cast<double>(input);
#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; }
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 using static_cast, C-style conversion, or functional-style conversion. In all three cases we have to mention the result data type before assignment.
Covers several methods of converting integers to double values. Programmers must determine which conversion method is best for a specific situation, because different conversion scenarios require different conversion methods. However, because implicit conversions occur automatically, programmers don't have to worry about enforcing complex strategies.
The above is the detailed content of C++ program to convert int variable to double. For more information, please follow other related articles on the PHP Chinese website!