. Advertisement .
..3..
. Advertisement .
..4..
C++ is, without any doubt, the single most robust programming language on the market. However, it’s also one of the hardest to master, and every single thing needs your manual intervention.
A prime example of this issue is learning to turn double to int in C++. While this issue is fairly manageable in other languages, it’s much harder in this language. That is why we prepared this guide.
How To Turn Double To Int In C++ Using round()
The most popular solution to this problem is arguably the round()
function, which is defined by C++’s cmath header. This function is capable of computing the closest int value from the user’s input argument.
Its rounding rule is that all halfway cases will be rounded away from 0. In other words, if you have a value larger or equal to 0.5, you will get a 1, while a 0.4 and anything below it becomes 0.
You do need to always put in the same data type before round()
as the input argument’s data type. Otherwise, you will certainly get an error statement.
This issue is not a problem if you are currently using C++ 11, as it provides additional overloads. They will turn your input into a double and then start the calculation.
Example:
#include <cmath>
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
using std::vector;
int main() {
vector<double> dfloats {-3.5, -21.1, -1.99, 0.129, 2.5, 3.111};
for (auto &item : dfloats) {
cout << "round(" << item << ") = " << round(item) << endl;
}
return EXIT_SUCCESS;
}
How To Turn Double To Int In C++ Using lround()
lround()
is another C++ function that has an almost identical working process as the round()
function. There is, however, a huge difference in the returned value. If round()
gives you back the value in the same data type, lround()
will only provide you with a long integer result.
There are only two types of error possible for this function, overflow range error and domain error. When you have the rounded value being outside of the long integer’s range, you get an overflow range error. On the other hand, if the result has an unspecified value, you get the domain error message.
Example:
#include <cmath>
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
using std::vector;
int main() {
vector<double> dfloats {-3.5, -21.1, -1.99, 0.129, 2.5, 3.111};
for (auto &item : dfloats) {
cout << "round(" << item << ") = " << lround(item) << endl;
}
return EXIT_SUCCESS;
}
How To Turn Double To Int In C++ Using trunc()
Both approaches that we have shown have flexible rounding mechanisms where they will round to the nearest zero. However, doing so means that the returned value can become larger in magnitude than the input value.
This problem can be quite serious if you are dealing with circumstances where having a bigger number leads to a fatal error for the code. For example, you can’t use it for timer codes.
If you are not a fan of this issue, you can consider the trunc()
method, which will truncate the input value. In other words, it will round the input to zero, but it will only return the nearest integer with a smaller value than the input.
We do want to note that this method is only good in specific situations. In a broader sense, it cannot provide you with results as correct as the round()
or lround()
methods.
Example:
#include <cmath>
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
using std::vector;
int main() {
vector<double> dfloats {-3.5, -21.1, -1.99, 0.129, 2.5, 3.111};
for (auto &item : dfloats) {
cout << "round(" << item << ") = " << trunc(item) << endl;
}
return EXIT_SUCCESS;
}
Conclusion
At the end of the day, each of the three methods to turn double to int in C++ has its own set of strengths and weaknesses. Once you manage to make use of their unique potential, rounding will never be a problem for you again.
As we mentioned, C++ is one of the most robust engines for this type of thing, as long as you can master it.
Leave a comment