Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

C Round Function

, 12 Feb 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

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.

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.

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.

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:

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

License

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

Share

About the Author

Ali BaderEddin
Software Developer Microsoft
United States United States
http://mycodelog.com/about/

Comments and Discussions

 
QuestionMy variant... PinmemberYoung-Jin Chung8-Aug-14 13:52 
GeneralMy vote of 4 Pinmemberhuzemin27-Oct-10 2:26 
AnswerRound Half to Even Pinmembertyjnfghnfgsdf16-Oct-10 17:18 
GeneralMy vote of 5 Pinmembersarodgl11-Aug-10 7:02 
GeneralAlternative rounding function Pinmemberbasementman16-Feb-10 8:42 
GeneralRe: Alternative rounding function PinmemberAli BaderEddin16-Feb-10 8:58 
GeneralRe: Alternative rounding function PinmemberMember 448140722-Feb-10 15:42 
GeneralRe: Alternative rounding function PinmemberAli BaderEddin22-Feb-10 20:15 
GeneralRe: Alternative rounding function PinmemberMember 420917517-Mar-10 22:44 
GeneralRe: Alternative rounding function Pinmemberleon de boer6-Apr-10 7:16 
GeneralRe: Alternative rounding function PinmemberAli BaderEddin7-Apr-10 13:28 
GeneralMy rounding function PinmemberRobert Steed30-Jun-14 0:40 
Generalvote of 4 PinmemberRozis12-Feb-10 11:51 
GeneralRe: vote of 4 PinmemberAli BaderEddin12-Feb-10 12:56 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150301.1 | Last Updated 12 Feb 2010
Article Copyright 2010 by Ali BaderEddin
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid