Convert Int to Double in C++
Convert Int to Double in C++ (5 Programs).
1#include <iostream>2using namespace std;34int main() {5 int num = 123;67 // Method 1: Direct assignment8 double d1 = num;910 // Method 2: Using static_cast11 double d2 = static_cast<double>(num);1213 // Method 3: Explicit casting14 double d3 = (double)num;1516 cout << "Integer: " << num << endl;17 cout << "Double (method 1): " << d1 << endl;18 cout << "Double (method 2): " << d2 << endl;19 cout << "Double (method 3): " << d3 << endl;2021 return 0;22}
Output
Integer: 123 Double (method 1): 123 Double (method 2): 123 Double (method 3): 123
What's going on
The program converts an integer (like 123) into a double (like 123.0). This conversion is necessary when:
Example:
Header File Used
This header provides:
1#include <iostream>
Declaring Variables
The program declares:
int num = 123;
Method 1: Direct Assignment (Implicit Conversion)
double d1 = num;
This is the simplest method using implicit conversion:
How it works:
Advantages:
Example:
int n = 123;
double d = n; // d = 123.0 (implicit conversion)
Method 2: Using static_cast (Explicit Conversion)
double d2 = static_cast<double>(num);
This method uses explicit C++ style casting:
How it works:
Advantages:
Example:
int n = 123;
double d = static_cast<double>(n); // Explicit conversion
Method 3: Explicit Casting (C-style)
double d3 = (double)num;
This method uses C-style casting:
How it works:
Advantages:
Disadvantages:
Other Methods (Mentioned but not shown in code)
Method 4: Using Multiplication
double d4 = num * 1.0;
Method 5: Using Division
double d5 = num / 1.0;
Displaying Results
The program prints:
Output:
Integer: 123
Double (method 1): 123
Double (method 2): 123
Double (method 3): 123
All three methods produce the same double value (displayed as 123, but stored as 123.0).
1cout << "Integer: " << num << endl;2cout << "Double (method 1): " << d1 << endl;3cout << "Double (method 2): " << d2 << endl;4cout << "Double (method 3): " << d3 << endl;
Why Convert Integer to Double?
Avoiding Integer Division
:
When dividing integers, the result is truncated:
int a = 5, b = 2;
int result = a / b; // result = 2 (truncated)
Converting to double preserves decimal precision:
double a = 5.0, b = 2.0;
double result = a / b; // result = 2.5 (exact)
Mathematical Operations
:
Some operations require floating-point precision:
Function Parameters
:
Some functions require double parameters:
double power = pow(2.0, 3.0); // Requires double arguments
Precision Considerations
Integer to Double Conversion
:
Range Considerations
:
Display Format
:
When to Use Each Method
-
Direct Assignment
: Best for simple cases - automatic and clean.
-
static_cast
: Best for explicit conversions - clear intent, type-safe, recommended.
-
C-style Cast
: Use only for legacy code or C compatibility.
-
Multiplication/Division
: Rarely needed - use only for specific clarity requirements.
Best Practice
: Use static_cast for explicit conversions to make your intent clear and code more maintainable.
Common Use Cases
Arithmetic Operations
:
double result = static_cast<double>(a) / b; // Avoid integer division
Function Calls
:
double sqrtValue = sqrt(static_cast<double>(num));
Accumulation
:
double sum = 0.0;
sum += static_cast<double>(value); // Ensure floating-point addition
return 0;
This ends the program successfully.