round()
Published Oct 24, 2022Updated Dec 21, 2022
Contribute to Docs
The round()
function returns the integer that is closest to the argument, with halfway cases rounded away from the ending zero.
Syntax
round(num);
- The
num
parameter must be adouble
,float
, orlong double
. - The return value will be an integer.
- If the decimal in
num
is0.5
or higher, the closest integer greater thannum
is returned.
Example
The following example showcases the round()
function being applied to two double
values, one of which is a halfway case:
#include <iostream>#include <cmath>int main() {double num1 = 9.23;double result1;result1 = std::round(num1);std::cout << "The result of round(9.23) is " << result1 << "\n";double num2 = 4.5;double result2;result2 = std::round(num2);std::cout << "The result of round(4.5) is " << result2 << "\n";}
This produces the following output:
The result of round(9.23) is 9The result of round(4.5) is 5
Codebyte Example
The following example is runnable and rounds the halfway case away from zero:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.