#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
Convert Double to Int in C++
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.
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)
Methods for Conversion
Method 1: Direct Casting (Truncates)
cppint i1 = (int)num;
- Cuts off the decimal part
- Always rounds toward zero (truncates)
- For positive numbers: rounds down (123.789 → 123)
Method 2: Using static_cast
cppint i2 = static_cast<int>(num);
- Same behavior as (int) - truncates the decimal part
- More type-safe than C-style casting
- Recommended for explicit conversions in modern C++
Method 3: Using floor() (Rounds Down)
cppint i3 = floor(num);
- Always rounds down to the nearest integer
- Works correctly for both positive and negative numbers
- For positive numbers: same as truncation (123.789 → 123)
Method 4: Using round() (Rounds to Nearest)
cppint i4 = round(num);
- Rounds to the nearest integer
- Standard rounding: .5 and above rounds up, below .5 rounds down
- Most intuitive for human expectations
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.
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.