Click here to Skip to main content
15,867,686 members
Articles / All Topics

C Round Function

Rate me:
Please Sign up or sign in to vote.
4.62/5 (8 votes)
12 Feb 2010CPOL 149K   14   15
It's really weird that the C math library (math.h) doesn't support the round function. This post shows how to create one in C. Pretty simple, but can save you little thinking :)

It’s really weird that the C math library (math.h) doesn’t support the round function. It only includes the floor function which rounds down a float to an integer (can also be done by implicit or explicit casting) and the ceil function which rounds the value up.

For example:

C++
int x;

x = floor(1.2);   //  x is set to 1
x = floor(1.8);   //  x is set to 1
x = (int)1.8;     //  x is set to 1 (Explicit Narrowing Conversion)
x = 1.8;          //  x is set to 1 (Implicit Narrowing Conversion)
x = ceil(1.2);    //  x is set to 2
x = ceil(1.8);    //  x is set to 2

The round function is supposed to round the float value to the nearest integer.

C++
x = round(1.2);    //  x is set to 1
x = round(1.8);    //  x is set to 2

This can be done adding a 0.5 to the value and then truncating it.

C++
x = (int)(1.2 + 0.5);  //  x is set to 1
x = (int)(1.8 + 0.5);  //  x is set to 2

We also have to take negative values into consideration by adding -0.5 and then truncating.

C++
x = (int)(-1.2 - 0.5);  //  x is set to -1
x = (int)(-1.8 - 0.5);  //  x is set to -2

Thus, here is the resulting C function:

C++
int round(double number)
{
    return (number >= 0) ? (int)(number + 0.5) : (int)(number - 0.5);
}

Note that you might want to use long rather than int to include support for larger numbers and avoid integer overflow.

That’s it, pretty much primitive, but fun!

Enjoy!
Ali B

This article was originally posted at http://mycodelog.com/2008/02/13/c-round-function

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
https://open-gl.com

Comments and Discussions

 
QuestionMy variant... Pin
Young-Jin Chung8-Aug-14 12:52
Young-Jin Chung8-Aug-14 12:52 
GeneralMy vote of 4 Pin
huzemin27-Oct-10 1:26
huzemin27-Oct-10 1:26 
AnswerRound Half to Even Pin
tyjnfghnfgsdf16-Oct-10 16:18
tyjnfghnfgsdf16-Oct-10 16:18 
GeneralMy vote of 5 Pin
sarodgl11-Aug-10 6:02
sarodgl11-Aug-10 6:02 
GeneralAlternative rounding function Pin
basementman16-Feb-10 7:42
basementman16-Feb-10 7:42 
GeneralRe: Alternative rounding function Pin
Ali BaderEddin16-Feb-10 7:58
Ali BaderEddin16-Feb-10 7:58 
GeneralRe: Alternative rounding function Pin
Member 448140722-Feb-10 14:42
Member 448140722-Feb-10 14:42 
GeneralRe: Alternative rounding function Pin
Ali BaderEddin22-Feb-10 19:15
Ali BaderEddin22-Feb-10 19:15 
Neat.. However, having the array of 20 chars might not be a good idea if the function is used extensively...

Thanks for sharing Smile | :)

-- Ali B

http://mycodelog.com
GeneralRe: Alternative rounding function Pin
Member 420917517-Mar-10 21:44
Member 420917517-Mar-10 21:44 
GeneralRe: Alternative rounding function Pin
leon de boer6-Apr-10 6:16
leon de boer6-Apr-10 6:16 
GeneralRe: Alternative rounding function Pin
Ali BaderEddin7-Apr-10 12:28
Ali BaderEddin7-Apr-10 12:28 
GeneralRe: Alternative rounding function Pin
Member 141100252-Feb-20 22:30
Member 141100252-Feb-20 22:30 
GeneralMy rounding function Pin
Robert Steed29-Jun-14 23:40
Robert Steed29-Jun-14 23:40 
Generalvote of 4 Pin
Rozis12-Feb-10 10:51
Rozis12-Feb-10 10:51 
GeneralRe: vote of 4 Pin
Ali BaderEddin12-Feb-10 11:56
Ali BaderEddin12-Feb-10 11:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.