Round Me Up, Round Me Down

The C language library features a few rounding functions, but most of them deal with trimming floating point values down (or up) to integers. The other day I encountered an interesting problem: How to round a value up or down to the nearest value of 5.

Being a geek, I figured the formula would somehow involve the modulus operator, %, which naturally lops off values. But rather than write a simple equation, I decided to devise a function. Further, I decided to craft that function so it could round up or down to any integer digit. The result is the roundit() function, illustrated in this code:

#include <stdio.h>
#include <stdlib.h>

int roundit(int value, int limit)
{
    if( limit < 0)           /* round down */
    {
        limit = abs(limit);     /* make limit positive */
        return(value - (value%limit));
    }
    else                        /* round up */
    {
        return(value + limit - (value%limit));
    }
}

int main()
{
    int x;

    puts("+5\t+3\tval\t-3\t-5");
    for(x=0;x<20;x++)
    {
        printf("%2d\t%2d\t%2d\t%2d\t%2d\n",
            roundit(x,5),
            roundit(x,3),
            x,
            roundit(x,-3),
            roundit(x,-5)
            );
    }
    return(0);
}

The key to making the roundit() function work is the value%limit operation. The modulus operator returns a remainder value, which can then be added or subtracted to the original value yield the rounding result desired.

When the value is to be rounded down, the limit variable is negative. The if statement catches that condition as the first line in the roundit() function. The value of limit is then made positive, thanks to the abs(), absolute value, function. The return statement then takes the modulo value and subtracts it from the value sent. That operation rounds value down.

To round value up, the modulo is calculated and then subtracted from the limit value. That way, the code adds the result to the value passed, rounding it up.

The code’s output demonstrates how a value can be rounded up or down by 5 or 3. The original value is in the center column:

+5	+3	val	-3	-5
 5	 3	 0	 0	 0
 5	 3	 1	 0	 0
 5	 3	 2	 0	 0
 5	 6	 3	 3	 0
 5	 6	 4	 3	 0
10	 6	 5	 3	 5
10	 9	 6	 6	 5
10	 9	 7	 6	 5
10	 9	 8	 6	 5
10	12	 9	 9	 5
15	12	10	 9	10
15	12	11	 9	10
15	15	12	12	10
15	15	13	12	10
15	15	14	12	10
20	18	15	15	15
20	18	16	15	15
20	18	17	15	15
20	21	18	18	15
20	21	19	18	15

So to round the value 11 up by 5, you get 15. To round 11 down to the nearest value of 5, you get 10. That’s the way the roundit() function works.

2 thoughts on “Round Me Up, Round Me Down

  1. Thanks. abs() function is new to me.

    In Code::Blocks with GNU GCC Compiler, I had to add stdlib.h to make the program run.

  2. Whoops! My fault. The abs() function does require the stdlib.h header. Thanks for catching that.

    I remember abs() as one of the first functions I ever learned, but in the BASIC programming language. It’s pretty simple to understand, which is probably why they taught it first.

Leave a Reply