In C, variables of type int can only hold positive or negative integer values; they cannot hold decimal values. There are float and double values available for this purpose. The double data type was created to store decimals up to seven digits after the decimal point. Conversion of an integer to a double data type can be done automatically by the compiler (called an "implicit" conversion), or it can be explicitly requested by the programmer from the compiler (called an "explicit" conversion). In the following sections, we'll cover various conversion methods.
The compiler automatically performs implicit type conversion. To achieve this, two variables are required - one of floating point type and the other of integer type. When we simply assign a floating point value or variable to an integer variable, the compiler takes care of all the other things. This conversion suffers from data loss because integer variables cannot contain decimal values after the decimal point.
double input = <double value>; int output = input;
#include <iostream> using namespace std; int solve(double value) { int opVal = value; return opVal; } int main() { double ip = 25.3056; int op = solve(ip); cout<< "The input value is: " << ip << endl; cout<< "The output value is: " << op << endl; return 0; }
The input value is: 25.3056 The output value is: 25
As we can see, the conversion process is quite simple. We just assign the input variables to the output variables; no additional procedures are required. Also, you can see that the fractional part of the double value is not present in the output.
When the programmer explicitly instructs the compiler to convert one data type to another, this is called an explicit conversion or explicit type conversion. There are two ways to achieve this: one is to explicitly declare the data type during assignment, and the other is to use static_cast. We discussed the first method before.
There are two different execution methods. One is a C-style version and the other is a functional-style conversion.
The result data type is specified before the source variable, enclosed in parentheses.
double input = <double value>; int output = (int) input;
#include <iostream> using namespace std; int solve(double value) { int opVal = (int)value; return opVal; } int main() { double ip = 84.4439; int op = solve(ip); cout<< "The value before conversion: " << ip << endl; cout<< "The value after conversion: " << op << endl; return 0; }
The value before conversion: 84.4439 The value after conversion: 84
When providing parameters to a function, we declare the result data type and enclose the source value in parentheses.
double input = <double value>; int output = int(input);
#include <iostream> using namespace std; int solve(double value) { int opVal = int(value); return opVal; } int main() { double ip = -993.6571; int op = solve(ip); cout<< "The value before conversion: " << ip << endl; cout<< "The value after conversion: " << op << endl; return 0; }
The value before conversion: -993.657 The value after conversion: -993
To convert between predefined types, use static casts. Additionally, this cast, which can also be referenced explicitly, is responsible for enforcing the implicit type conversion.
double input = < double value>; int output = static_cast<int>(input);
#include <iostream> using namespace std; int solve(double value) { int opVal = static_cast<int>(value); return opVal; } int main() { double ip = -65.2354; int op = solve(ip); cout<< "The value before conversion: " << ip << endl; cout<< "The value after conversion: " << op << endl; return 0; }
The value before conversion: -65.2354 The value after conversion: -65
Converting from a double to an integer data type always results in data loss because an integer variable cannot contain the fractional part of a double variable. These conversions are useful when we have to round a value to its lower bound value (the smallest integer value given a decimal value).
The above is the detailed content of C++ program to convert double type variable to int type. For more information, please follow other related articles on the PHP Chinese website!