Convert Int to Double

Convert Int to Double in C++

C++Beginner
C++
#include <iostream>
using namespace std;

int main() {
    int num = 123;
    
    // Method 1: Direct assignment
    double d1 = num;
    
    // Method 2: Using static_cast
    double d2 = static_cast<double>(num);
    
    // Method 3: Explicit casting
    double d3 = (double)num;
    
    cout << "Integer: " << num << endl;
    cout << "Double (method 1): " << d1 << endl;
    cout << "Double (method 2): " << d2 << endl;
    cout << "Double (method 3): " << d3 << endl;
    
    return 0;
}

Output

Integer: 123
Double (method 1): 123
Double (method 2): 123
Double (method 3): 123

Convert Int to Double in C++

This program teaches you how to convert an integer to a double (floating-point number) in C++. This conversion is straightforward because integers can be represented exactly as doubles without loss of information. Understanding different conversion methods helps you write clear, type-safe code and understand implicit vs. explicit type conversions.

What This Program Does

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)

Methods for Conversion

Method 1: Direct Assignment (Implicit Conversion)

cpp
double d1 = num;
  • Simplest method using implicit conversion
  • C++ automatically converts the integer to double when assigning
  • No explicit cast needed

Method 2: Using static_cast (Explicit Conversion)

cpp
double d2 = static_cast<double>(num);
  • Recommended way for explicit type conversion in C++
  • Makes the conversion intention clear and explicit
  • Performs compile-time type checking

Method 3: Explicit Casting (C-style)

cpp
double d3 = (double)num;
  • C-style cast operator
  • Works the same as static_cast for this conversion
  • Less preferred in modern C++

Why Convert Integer to Double?

Avoiding Integer Division:

When dividing integers, the result is truncated:

cpp
int a = 5, b = 2;
int result = a / b;  // result = 2 (truncated)

Converting to double preserves decimal precision:

cpp
double a = 5.0, b = 2.0;
double result = a / b;  // result = 2.5 (exact)

Summary

  • Converting integers to doubles is straightforward and safe.
  • Direct assignment uses implicit conversion - simple and automatic.
  • static_cast is the recommended method for explicit conversions - clear and type-safe.
  • Integer to double conversion never loses precision (integers are exactly representable).
  • Use explicit conversion when you need to ensure floating-point arithmetic or pass to functions requiring double.

This program is essential for beginners learning type conversions, understanding implicit vs. explicit casting, and avoiding common pitfalls like integer division truncation in C++ programs.