Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

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.

BeginnerString Conversion ProgramsExample 8 of 11
convert-int-to-double.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 int num = 123;
6
7 // Method 1: Direct assignment
8 double d1 = num;
9
10 // Method 2: Using static_cast
11 double d2 = static_cast<double>(num);
12
13 // Method 3: Explicit casting
14 double d3 = (double)num;
15
16 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;
20
21 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. 1

    num holds 123.

  2. 2

    d1 = num converts implicitly.

  3. 3

    d2 uses static_cast, d3 a C-style cast.

  4. 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:

You need to perform floating-point arithmetic with integer values
You want to ensure decimal precision in calculations
You're working with functions that require double parameters
You need to avoid integer division truncation

Example:

Input integer: 123
Output double: 123.0 (or displayed as 123)

Header File Used

#include <iostream>

This header provides:

cout for displaying output
cin for taking input (not used in this example, but available)

Declaring Variables

The program declares:

int num = 123;

int is the data type for storing whole numbers.
num stores the integer value 123 that we want to convert to a double.

Method 1: Direct Assignment (Implicit Conversion)

double d1 = num;

This is the simplest method using implicit conversion:

C++ automatically converts the integer to double when assigning.
No explicit cast needed - the compiler handles it automatically.
The integer 123 becomes double 123.0.

How it works:

When you assign an int to a double, C++ performs an implicit conversion.
The integer value is converted to its floating-point equivalent.
No data loss occurs (integers can be exactly represented as doubles).

Advantages:

Simplest syntax
Automatic and safe
No explicit casting needed

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:

static_cast is the recommended way for explicit type conversion in C++.
Makes the conversion intention clear and explicit.
Performs compile-time type checking.

How it works:

static_cast<double>(123) explicitly converts the integer to double.
The compiler verifies that the conversion is valid.
More explicit than implicit conversion.

Advantages:

Clear intent - shows you're intentionally converting types
Type-safe - compiler checks for valid conversions
Recommended for explicit conversions in modern C++
Better code readability

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:

(double) is the C-style cast operator.
Works the same as static_cast for this conversion.
Less preferred in modern C++.

How it works:

(double)num casts the integer to double.
Similar to static_cast but C-style syntax.

Advantages:

Simple and familiar (especially for C programmers)
Works correctly for this conversion

Disadvantages:

Less safe than static_cast (no compile-time checks for some conversions)
Less explicit about conversion intent
Not recommended for complex type conversions

Other Methods (Mentioned but not shown in code)

Method 4: Using Multiplication

double d4 = num * 1.0;

Multiplying by 1.0 (a double literal) forces conversion.
The result of int * double is double.
Less direct but sometimes used for clarity.

Method 5: Using Division

double d5 = num / 1.0;

Dividing by 1.0 (a double literal) forces conversion.
Similar to multiplication method.
Less common approach.

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:

Square roots: sqrt(4) requires double
Trigonometric functions: sin(), cos() require double
Logarithmic functions: log() requires double

Function Parameters:

Some functions require double parameters:

double power = pow(2.0, 3.0); // Requires double arguments

Precision Considerations

Integer to Double Conversion:

Integers can be exactly represented as doubles (no precision loss).
Example: 123 (int) → 123.0 (double) - exact conversion.

Range Considerations:

int typically ranges from -2,147,483,648 to 2,147,483,647.
double can represent much larger and smaller values.
No overflow issues when converting int to double.

Display Format:

When displaying, cout may show 123.0 as "123" (trailing zeros omitted).
Use formatting to control decimal places if needed.

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.

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.