Round floating-point value to int in C++
This post will discuss how to round floating-point value to the nearest int in C++.
1. Using std::round
Starting with C++11, the std::round function is the most elegant way to round a floating-point value to the nearest int. If your compiler supports it, you can use it as follows:
|
1 2 3 4 5 6 7 8 9 10 11 |
#include <iostream> #include <cmath> int main() { std::cout << std::round(3.49) << std::endl; // 3 std::cout << std::round(3.50) << std::endl; // 4 std::cout << std::round(3.51) << std::endl; // 4 return 0; } |
The std::round function is defined in the <cmath> header (in C++11). You may also use the std::lround and std::llround function to round the specified floating-point value to the nearest integer value, returned as long integer and long long integer, respectively.
2. Using std::floor
There’s no round function in the C++98 standard library. However, you can easily write your rounding function with std::floor function. The following code implements round-half-up logic:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> #include <cmath> double round(double d) { return std::floor(d + 0.5); } int main() { std::cout << round(3.49) << std::endl; // 3 std::cout << round(3.50) << std::endl; // 4 std::cout << round(3.51) << std::endl; // 4 return 0; } |
3. Using boost
Another option is to use the rounding functions offered by the boost library. There are several functions in the header <boost/math/special_functions/round.hpp> – that return the closest integer to the specified argument.
T round(const T& v);
int iround(const T& v);
long lround(const T& v);
long long llround(const T& v);
Note that halfway cases are rounded away from zero, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 |
#include <iostream> #include <boost/math/special_functions/round.hpp> int main() { std::cout << boost::math::round(3.49) << std::endl; // 3 std::cout << boost::math::round(3.50) << std::endl; // 4 std::cout << boost::math::round(3.51) << std::endl; // 4 return 0; } |
4. Using std::trunc
If you need to truncate the value to the nearest integer, instead of rounding it, consider using the std::trunc function. This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 |
#include <iostream> #include <cmath> int main() { std::cout << std::trunc(3.49) << std::endl; // 3 std::cout << std::trunc(3.50) << std::endl; // 3 std::cout << std::trunc(3.51) << std::endl; // 3 return 0; } |
Another possibility is using static cast which provides a safe way to convert from one type to another. You can do something like:
|
1 2 3 4 5 6 7 8 9 10 11 |
#include <iostream> #include <cmath> int main() { std::cout << static_cast<int>(3.49) << std::endl; // 3 std::cout << static_cast<int>(3.50) << std::endl; // 3 std::cout << static_cast<int>(3.51) << std::endl; // 3 return 0; } |
That’s all about rounding a floating-point value to the nearest int in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)