Convert Double to Int
Convert Double to Int in C++ (5 Programs)
C++ Convert Double to Int Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#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;
}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:
Example:
---
2. Header Files Used
---
3. Declaring Variables
The program declares:
double num = 123.789;
---
4. 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)
---
5. 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)
---
6. 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)
---
7. 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)
---
8. 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)
---
9. Displaying Results
The program prints:
Output:
---
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)
:
For Negative Numbers (-123.789)
:
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:
Example
:
double d = 123.789;
int i = round(d); // i = 124
---
// Original 123.789 is lost - cannot get it back from i14. return 0;
This ends the program successfully.
---
Summary
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.