Page 1 of 1

How to Round up/Round Down to the nearest 5?

PostPosted: Mon May 22, 2006 4:42 pm
by laddsa
Hi,

I want a way of rounding up or rounding down to the nearest 5. For example, if I have the number 23, then it would be rounded up to 25. If I have the number 21, then it would be rounded down to 20. Is there a function/code in Game-Editor that can do this. I've looked at the 'Round' function and it doesn't seem possible.

Thanks,

Laddsa

PostPosted: Mon May 22, 2006 6:19 pm
by makslane
Put this function in the Global Code editor:

Code: Select all
int roundSnap(int x, int snap)
{
   int r;

   r = x - (x % snap);
   r = (x - r > snap/2)?(r + snap):r;

   return r;
}



And use like this:
out = roundSnap(inputvalue, 5);

That worked!

PostPosted: Mon May 22, 2006 7:02 pm
by laddsa
Thanks very much for the really rapid response Makslane! - that worked great!!

Laddsa