Convert Int to Double in C++
Convert an int to a double in C++ by plain assignment, static_cast, or a C-style cast. Shows why 123 prints without .0 and how casting fixes integer division.
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}
Sample Output
Double (method 1): 123
Stored as 123.0, printed without decimals.
In this one
- That assigning an int to a double converts it automatically
- How static_cast<double> makes the conversion explicit
- Why cout prints 123 instead of 123.0
- How casting one operand changes 7 / 2 to 3.5
How it works
- 1
num holds 123.
- 2
d1 = num converts implicitly.
- 3
d2 uses static_cast, d3 a C-style cast.
- 4
All three hold 123.0, which cout prints as 123.
Edge cases
Casting after division
Write double r = (double)(7 / 2);
7 / 2 is done first in int, giving 3, then converted to 3.0. Cast an operand instead: (double)7 / 2 gives 3.5.
Very large long long
Convert 9007199254740993LL to double.
double has 53 bits of precision, so this value rounds to 9007199254740992. Plain int values always convert exactly.
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
#include <iostream>
This header provides:
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:
cout << "Integer: " << num << endl;
cout << "Double (method 1): " << d1 << endl;
cout << "Double (method 2): " << d2 << endl;
cout << "Double (method 3): " << d3 << endl;
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).
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
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.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What is (double)5 / 2?
Pick an answer to check it.
Question 2
What does cout << double(42); print?
Pick an answer to check it.
Questions people ask
Why does 123.0 print as 123?
cout removes trailing zeros by default. Use fixed and setprecision to show decimals.
Is implicit conversion safe?
From int to double, yes for every 32-bit int value.
Why use static_cast at all?
It shows the reader the conversion is intentional.