Convert Double to Int

Convert Double to Int in C++ (5 Programs)

BeginnerTopic: String Conversion Programs
Back

C++ Convert Double to Int Program

This program helps you to learn the fundamental structure and syntax of C++ programming.

Try This Code
#include <iostream>
#include <cmath>
using namespace std;

int main() {
    double num = 123.789;
    
    // Method 1: Direct casting (truncates)
    int i1 = (int)num;
    
    // Method 2: Using static_cast
    int i2 = static_cast<int>(num);
    
    // Method 3: Using floor()
    int i3 = floor(num);
    
    // Method 4: Using round()
    int i4 = round(num);
    
    cout << "Double: " << num << endl;
    cout << "Integer (cast): " << i1 << endl;
    cout << "Integer (static_cast): " << i2 << endl;
    cout << "Integer (floor): " << i3 << endl;
    cout << "Integer (round): " << i4 << endl;
    
    return 0;
}
Output
Double: 123.789
Integer (cast): 123
Integer (static_cast): 123
Integer (floor): 123
Integer (round): 124

Understanding Convert Double to Int

This program teaches you how to convert a double (floating-point number) to an integer in C++. This conversion is important because it involves losing the decimal part, and different methods handle this differently - some truncate (cut off), some round down, some round to nearest, and some round up. Understanding these differences is crucial for accurate calculations and proper data handling.

---

1. What This Program Does

The program converts a double (like 123.789) into an integer. Since integers cannot store decimal values, the decimal part must be handled. Different methods handle this differently:

Truncation: Cuts off the decimal part (123.789 → 123)
Floor: Rounds down to the nearest integer (123.789 → 123)
Round: Rounds to the nearest integer (123.789 → 124)
Ceil: Rounds up to the nearest integer (123.789 → 124)

Example:

Input double: 123.789
Output depends on method: 123 (truncate/floor) or 124 (round/ceil)

---

2. Header Files Used

1.#include <iostream>
Provides cout and cin for input/output operations.
2.#include <cmath>
Provides mathematical functions: floor(), round(), ceil().
Essential for rounding operations.

---

3. Declaring Variables

The program declares:

double num = 123.789;

double is the data type for storing decimal numbers with high precision.
num stores the double value 123.789 that we want to convert to an integer.

---

4. Method 1: Direct Casting (Truncates)

int i1 = (int)num;

This method uses C-style casting and truncates:

(int) casts the double to integer, cutting off the decimal part.
123.789 becomes 123 (decimal part .789 is discarded).
Always rounds toward zero (truncates).

How it works:

The cast operator (int) extracts only the integer part.
Decimal portion is completely removed, not rounded.
For positive numbers: rounds down (123.789 → 123).
For negative numbers: rounds up toward zero (-123.789 → -123).

Advantages:

Simple and fast
Familiar syntax

Disadvantages:

No rounding - just cuts off decimal part
May not give the desired result for calculations

Example:

double d = 123.789;

int i = (int)d; // i = 123 (truncated)

---

5. Method 2: Using static_cast

int i2 = static_cast<int>(num);

This method uses C++ style casting and also truncates:

static_cast<int> explicitly converts double to integer.
Same behavior as (int) - truncates the decimal part.
More type-safe than C-style casting.

How it works:

static_cast<int>(123.789) converts to 123.
Truncates the same way as C-style cast.
Recommended for explicit conversions in modern C++.

Advantages:

Type-safe - compiler checks conversion validity
Clear intent - shows explicit conversion
Recommended for modern C++

Example:

double d = 123.789;

int i = static_cast<int>(d); // i = 123 (truncated)

---

6. Method 3: Using floor() (Rounds Down)

int i3 = floor(num);

This method rounds down to the nearest integer:

floor() from <cmath> always rounds down.
123.789 becomes 123 (rounded down).
Works correctly for both positive and negative numbers.

How it works:

floor(123.789) returns 123.0 (as double), then cast to int.
Always rounds toward negative infinity.
For positive numbers: same as truncation (123.789 → 123).
For negative numbers: different from truncation (-123.789 → -124).

Advantages:

Consistent rounding behavior
Useful when you always want to round down

Example:

double d = 123.789;

int i = floor(d); // i = 123 (rounded down)

double d2 = -123.789;

int i2 = floor(d2); // i2 = -124 (rounded down, not -123)

---

7. Method 4: Using round() (Rounds to Nearest)

int i4 = round(num);

This method rounds to the nearest integer:

round() from <cmath> rounds to the nearest integer.
123.789 becomes 124 (rounded to nearest).
Standard rounding: .5 and above rounds up, below .5 rounds down.

How it works:

round(123.789) returns 124.0 (as double), then cast to int.
Uses standard mathematical rounding rules.
Most intuitive for human expectations.

Advantages:

Most intuitive rounding method
Standard mathematical rounding
Best for displaying results to users

Example:

double d = 123.789;

int i = round(d); // i = 124 (rounded to nearest)

double d2 = 123.4;

int i2 = round(d2); // i2 = 123 (rounded down)

---

8. Other Methods (Mentioned but not shown in code)

Method 5: Using ceil() (Rounds Up)

int i5 = ceil(num);

ceil() from <cmath> always rounds up.
123.789 becomes 124 (rounded up).
Always rounds toward positive infinity.
Useful when you need to ensure a minimum value.

Example:

double d = 123.1;

int i = ceil(d); // i = 124 (rounded up)

---

9. Displaying Results

The program prints:

Output:

Double: 123.789
Integer (cast): 123
Integer (static_cast): 123
Integer (floor): 123
Integer (round): 124
Notice: cast, static_cast, and floor all give 123 (truncate/down), while round gives 124 (nearest).

---

cout << "Double: " << num << endl;
cout << "Integer (cast): " << i1 << endl;
cout << "Integer (static_cast): " << i2 << endl;
cout << "Integer (floor): " << i3 << endl;
cout << "Integer (round): " << i4 << endl;

10. Understanding the Differences

For Positive Numbers (123.789)

:

Cast/static_cast: 123 (truncates)
floor(): 123 (rounds down)
round(): 124 (rounds to nearest)
ceil(): 124 (rounds up)

For Negative Numbers (-123.789)

:

Cast/static_cast: -123 (truncates toward zero)
floor(): -124 (rounds down)
round(): -124 (rounds to nearest)
ceil(): -123 (rounds up)

Key Difference

: For negative numbers, truncation and floor() behave differently!

---

11. When to Use Each Method

-

Cast/static_cast

: When you want to truncate (cut off decimals) - simple and fast.

-

floor()

: When you always want to round down - useful for calculations requiring minimum values.

-

round()

: When you want standard rounding - best for user-facing results and most calculations.

-

ceil()

: When you always want to round up - useful for calculations requiring maximum values.

Best Practice

: Use round() for most cases where you want intuitive rounding behavior.

---

12. Common Use Cases

Displaying Results

:

int displayValue = round(result); // User-friendly rounding

Array Indexing

:

int index = floor(position); // Always round down for indices

Calculating Quantities

:

int items = ceil(total / perItem); // Round up to ensure enough items

Truncating Decimals

:

int wholePart = static_cast<int>(value); // Just remove decimal part

---

13. Precision Loss Warning

Important

: Converting double to int loses precision:

The decimal part is permanently lost.
Cannot recover the original double from the integer.
Always consider if you really need an integer or should keep it as double.

Example

:

double d = 123.789;

int i = round(d); // i = 124

---

// Original 123.789 is lost - cannot get it back from i

14. return 0;

This ends the program successfully.

---

Summary

Converting doubles to integers requires handling the decimal part.
Direct casting truncates (cuts off) the decimal part.
floor() always rounds down to the nearest integer.
round() rounds to the nearest integer (most intuitive).
ceil() always rounds up to the nearest integer.
For negative numbers, truncation and floor() behave differently.
Choose the method based on your specific rounding needs.
Always be aware that precision is lost in the conversion.

This program is essential for beginners learning type conversions, understanding different rounding methods, and making informed decisions about when and how to convert floating-point numbers to integers in C++ programs.

Let us now understand every line and the components of the above program.

Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.

Practical Learning Notes for Convert Double to Int

This C++ program is part of the "String Conversion Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.

A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.

For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.

Table of Contents