
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert Double Type Variables to Int in C++
In C++, variables of the int type can only hold positive or negative integer values; they cannot hold fractional values. There are float and double values available for this purpose. In order to store fractional numbers with up to seven digits after the decimal point, double datatype was created. Integer to double datatype conversion can either be done automatically by the compiler (known as "implicit" conversion) or explicitly requested by the programmer to the compiler (known as "explicit" conversion). In the sections that follow, we go over the various conversion methods.
Implicit Conversion
The compiler does implicit type conversions automatically. Two variables-one of float type and the other of integer type-are needed for this to happen. The compiler will take care of everything else when we simply assign the float value or variable to the integer variable. There is a problem of data loss associated in this conversion, as integer variables cannot contain the fractional values after the decimal point.
Syntax
double input = <double value>; int output = input;
Algorithm
- Take a double value as the input;
- Assign the value to an integer variable.
- Display the output.
Example
#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; }
Output
The input value is: 25.3056 The output value is: 25
The converting procedure is rather simple, as we can see. We just assign the input variable to the output variable; no extra procedures are required. Also, it is to be seen that the fractional part of the double value is absent from the output.
Explicit conversion
When a programmer explicitly instructs the compiler to convert one data type to another, this is known as explicit conversion or explicit type casting. There are two ways to accomplish this: one is by explicitly stating the data type at assignment, and the other is by using static_cast. We discuss the first approach before.
Algorithm
- Take a double value as the input;
- Assign the value to an integer variable using explicit type casting.
- Display the output.
Mentioning datatype during assignment
There are two different ways to be performed. One is the C-styled version and another one is the function-styled casting.
C-Styled version
The resultant datatype is specified before the source variable and the is enclosed within brackets.
Syntax
double input = <double value>; int output = (int) input;
Example
#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; }
Output
The value before conversion: 84.4439 The value after conversion: 84
Function-styled casting
When supplying an argument to a function, we state the resultant datatype and wrap the source value inside of brackets.
Syntax
double input = <double value>; int output = int(input);
Example
#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; }
Output
The value before conversion: -993.657 The value after conversion: -993
Using static_cast
To convert between pre-defined types, static cast is used. Additionally, this cast which can also be referred to explicitly is in charge of enforcing implicit type conversion.
Syntax
double input = < double value>; int output = static_cast<int>(input);
Example
#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; }
Output
The value before conversion: -65.2354 The value after conversion: -65
Conclusion
Conversion from double to integer data types always result in data loss as integer variables cannot contain the fractional parts of a double variable. These conversions are useful when we have to round-off a value to its floor value, the smallest integer value to a given fractional value.