Convert Double to Int in C++
Convert Double to Int in C++ (5 Programs).
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}
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:
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:
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)
:
For Negative Numbers (-123.789)
:
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:
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.