Convert Double to Int in C++
Convert a double to an int in C++: truncate with a cast, round down with floor, or round to nearest with round. Compares results for 123.789 and negatives.
1#include <iostream>2#include <cmath>3using namespace std;45int main() {6 double num = 123.789;78 // Method 1: Direct casting (truncates)9 int i1 = (int)num;1011 // Method 2: Using static_cast12 int i2 = static_cast<int>(num);1314 // Method 3: Using floor()15 int i3 = floor(num);1617 // Method 4: Using round()18 int i4 = round(num);1920 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;2526 return 0;27}
Sample Output
Integer (cast): 123
Fraction dropped.
Integer (round): 124
Nearest integer.
In this one
- That casting to int truncates toward zero
- How floor rounds down and round rounds to nearest
- Why negative numbers make cast and floor differ
- What happens when the double is too big for int
How it works
- 1
num holds 123.789.
- 2
(int)num and static_cast<int> both give 123.
- 3
floor(num) gives 123.0, stored as 123.
- 4
round(num) gives 124.0, stored as 124.
- 5
Print all four.
Edge cases
Negative value
Use -2.5.
The cast truncates to -2, floor goes down to -3, and round goes away from zero to -3. Truncation and floor only agree for positives.
Too large
Convert 1e20.
The value does not fit in int, and converting it is undefined behavior. Check the range before casting.
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:
Example:
Header Files Used
Declaring Variables
The program declares:
double num = 123.789;
Method 1: Direct Casting (Truncates)
int i1 = (int)num;
This method uses C-style casting and truncates:
How it works:
Advantages:
Disadvantages:
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:
How it works:
Advantages:
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:
How it works:
Advantages:
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:
How it works:
Advantages:
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);
Example:
double d = 123.1;
int i = ceil(d); // i = 124 (rounded up)
Displaying Results
The program prints:
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;
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).
Understanding the Differences
For Positive Numbers (123.789):
For Negative Numbers (-123.789):
Key Difference: For negative numbers, truncation and floor() behave differently!
When to Use Each Method
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:
Example:
double d = 123.789;
int i = round(d); // i = 124
// Original 123.789 is lost - cannot get it back from i
return 0;
This ends the program successfully.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
(int)-3.7 equals:
Pick an answer to check it.
Question 2
round(4.5) returns:
Pick an answer to check it.
Questions people ask
Which one rounds 2.5 to 3?
round, which rounds halves away from zero.
What about ceil?
ceil rounds up: ceil(2.1) is 3.
Do floor and round return int?
No, they return double. Assigning to int converts the already-whole value.