Convert Double to Int in C++

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

BeginnerString Conversion ProgramsExample 9 of 11
convert-double-to-int.cpp
1#include <iostream>
2#include <cmath>
3using namespace std;
4
5int main() {
6 double num = 123.789;
7
8 // Method 1: Direct casting (truncates)
9 int i1 = (int)num;
10
11 // Method 2: Using static_cast
12 int i2 = static_cast<int>(num);
13
14 // Method 3: Using floor()
15 int i3 = floor(num);
16
17 // Method 4: Using round()
18 int i4 = round(num);
19
20 cout << "Double: " << num << endl;
21 cout << "Integer (cast): " << i1 << endl;
22 cout << "Integer (static_cast): " << i2 << endl;
23 cout << "Integer (floor): " << i3 << endl;
24 cout << "Integer (round): " << i4 << endl;
25
26 return 0;
27}

Output

Double: 123.789
Integer (cast): 123
Integer (static_cast): 123
Integer (floor): 123
Integer (round): 124

What's going on

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)

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.

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.

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)

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)

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)

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)

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)

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).

1cout << "Double: " << num << endl;
2cout << "Integer (cast): " << i1 << endl;
3cout << "Integer (static_cast): " << i2 << endl;
4cout << "Integer (floor): " << i3 << endl;
5cout << "Integer (round): " << i4 << endl;

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!

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.

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

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

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

return 0;

This ends the program successfully.