Example 1 — Round Several Numbers
Round numbers below, at, and above one half, and a negative half.
#include <cmath>
#include <iostream>
int main() {
double values[] = {2.4, 2.5, 2.6, -2.5};
for (double v : values) {
std::cout << "round(" << v << ") = " << std::round(v) << '\n';
}
return 0;
} How It Works
- The loop gives each number in the array to
std::round(). 2.4is closer to2, so it rounds down.2.6is closer to3, so it rounds up.2.5is exactly halfway and goes away from zero to3.-2.5also goes away from zero, to-3.

