In this blog, we will learn various methods to convert double to int in C++, with clear explanations and example programs.

## Introduction:

Converting data from one type to another is a common operation in programming. In C++, you might often find yourself needing to convert a double (a floating-point number) into an int (an integer). This process may seem straightforward, but there are several methods to consider, each with its own implications. In this blog, we will explore various methods for converting a double to an int in C++.

## Method 1: Casting

The first method for converting a double to an int is casting. In C++, casting is the process of changing the type of a variable explicitly. In this case, you can use a C-style cast or a more modern C++ cast like `static_cast`. Let's take a look at a simple example using casting:

``````#include <iostream>

int main() {
double doubleValue = 3.14159;
int intValue = static_cast<int>(doubleValue);

std::cout << "Double value: " << doubleValue << std::endl;
std::cout << "Int value (casted): " << intValue << std::endl;

return 0;
}
``````

### Output:

``````Double value: 3.14159
Int value (casted): 3
``````

In this method, we use the `static_cast` to explicitly cast the double value to an integer. The casting operation truncates the decimal part, effectively rounding towards zero. This is a straightforward method, but it may lead to loss of information since we are removing the fractional part. Use this method when you are confident that you want to discard the fractional part.

## Method 2: C++ Standard Library Functions

C++ provides several standard library functions to convert between data types. One such function is `std::floor` from the `<cmath>` library. `std::floor` returns the largest integer value that is less than or equal to the given double value. Here's an example:

``````#include <iostream>
#include <cmath>

int main() {
double doubleValue = 3.14159;
int intValue = std::floor(doubleValue);

std::cout << "Double value: " << doubleValue << std::endl;
std::cout << "Int value (using std::floor): " << intValue << std::endl;

return 0;
}
``````

### Output:

``````Double value: 3.14159
Int value (using std::floor): 3
``````

The `std::floor` function effectively rounds the double value down to the nearest integer. It's particularly useful when you need to ensure that the double value is always rounded towards negative infinity. This method is a better choice when you want to round down, even for negative values.

## Method 3: C++ Type Conversion Functions

C++ provides specific functions for type conversion, such as `std::stoi` and `std::stol`, which are used for converting strings to integers and longs, respectively. In our case, you can use `std::lround` from the `<cmath>` library to convert a double to the nearest long integer:

``````#include <iostream>
#include <cmath>

int main() {
double doubleValue = 3.14159;
int intValue = std::lround(doubleValue);

std::cout << "Double value: " << doubleValue << stdendl;
std::cout << "Int value (using std::lround): " << intValue << std::endl;

return 0;
}
``````

### Output:

``````Double value: 3.14159
Int value (using std::lround): 3
``````

`std::lround` is used to round a double value to the nearest integer, following the standard rounding rules. It's a better choice when you need to round to the nearest integer, regardless of the decimal part.

## Method 4: User-Defined Rounding Logic

In some cases, you may require custom rounding logic. For example, you might need to round up or down based on specific criteria. To achieve this, you can create your own rounding function. Let's look at an example where we round up to the nearest integer if the decimal part is greater than or equal to 0.5:

``````#include <iostream>
#include <cmath>

int customRound(double value) {
double fractionalPart = value - std::floor(value);
if (fractionalPart >= 0.5) {
return static_cast<int>(std::ceil(value));
} else {
return static_cast<int>(std::floor(value));
}
}

int main() {
double doubleValue = 3.67;
int intValue = customRound(doubleValue);

std::cout << "Double value: " << doubleValue << std::endl;
std::cout << "Int value (custom rounding): " << intValue << std::endl;

return 0;
}
``````

### Output:

``````Double value: 3.67
Int value (custom rounding): 4
``````

In this method, we define a custom rounding function `customRound` that rounds the double value up if the fractional part is greater than or equal to 0.5, and down otherwise. This allows you to implement custom rounding logic to suit your specific needs.

## Conclusion:

In C++, converting a double to an int is a common operation, but the choice of method can have a significant impact on your program's behavior. The method you choose should align with your requirements, whether you need to truncate the decimal part, round to the nearest integer, or implement custom rounding logic. Here's a quick recap of the methods we covered:

• Casting: Use this method when you want to truncate the decimal part and keep the integer portion.
• C++ Standard Library Functions: Functions like `std::floor` and `std::lround` provide standard rounding behavior and are suitable for most scenarios.
• User-Defined Rounding Logic: Create custom rounding functions when you have specific rounding requirements that are not covered by the standard functions. 