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

Game Editor comments and discussion.

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

Postby laddsa » Mon May 22, 2006 4:42 pm

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
laddsa
 
Posts: 22
Joined: Sun Jul 24, 2005 10:18 am
Score: 0 Give a positive score

Postby makslane » Mon May 22, 2006 6:19 pm

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);
makslane
Site Admin
 
Posts: 3947
Joined: Sat Apr 05, 2003 6:47 pm
Score: 182 Give a positive score

That worked!

Postby laddsa » Mon May 22, 2006 7:02 pm

Thanks very much for the really rapid response Makslane! - that worked great!!

Laddsa
laddsa
 
Posts: 22
Joined: Sun Jul 24, 2005 10:18 am
Score: 0 Give a positive score


Return to GE - General

Who is online

Users browsing this forum: No registered users and 1 guest