# rounding question

• 08-13-2003
keithmolo
rounding question
is it possible to round to the next highest multiple of four? for example:

if i have the values:

1.6, 2.3, 3.4, 3.9 - all of them need to be 4.

4.1, 5.6, 6.3 - all of them need to be 8.

8.1, 9.7, 10 - all of them need to be 12.

etc.

can the ceiling function do this? any suggestions?

keith
• 08-13-2003
PJYelton
Depending on how many significant digits you need to worry about, off the top of my head I would do something similar to this (although I'm sure there is an easier way!):
Code:

`x=int(x+3.999999)/4*4;`
• 08-13-2003
zMan
multiple of 4
you could create a function that does it for you

Code:

```int RoundBy4( float flValue ) {     int iFoursInValue;     int iValue;     iValue = flValue + 4;     iFoursInValue = iValue / 4;     iValue = iFoursInValue * 4;     return iValue; }```
something like this could work .... you probably have to do a little more work to it but it should work....
• 08-13-2003
Jubba
I would do this...

Code:

``` #include <math.h> double d = 2.4; int i = (int)ceil(2.4); while((++i)%4); // i is now round to highest multiple of 4```
• 08-13-2003
keithmolo
wow you guys are good. i just can't think like that ;)

thanks a bunch.

keith
• 08-13-2003
PJYelton
ZMan - yours unfortunately doesn't work for numbers that don't need to be rounded at all. For example, 4.0 should be 4, but yours will say 8. Thats why I added 3.99999 instead of 4.

Jubbas is probably best if you need to worry about an unlimited amount of numbers after the decimal point, but you'll have to change "while((++i)%4)" to "while((i++)%4)" otherwise something like 3.4 will come out as 8.
• 08-13-2003
Hunter2
Instead of using 3.99999, use an if. If number % 4 == 0, return number.
• 08-13-2003
PJYelton
But you can't use % with floats though.
• 08-13-2003
Hunter2
if((int)number % 4 == 0)

Sheesh... figure it out yourself, you've done typecasting before. Do I really have to spoonfeed you? :p
• 08-13-2003
PJYelton
LOL again doesn't work! Try that with 4.1! :D
• 08-13-2003
Hunter2
Oh yeah? well:
Code:

```if((((double)(int)number) == number) && ((int)number % 4 == 0))     return number; else     return ((double)((int)(number + 4)/4*4));```
HAH! :D
• 08-13-2003
golfinguy4
wtf are you thinking? Ever heard of fmod?
• 08-14-2003
Hunter2
Yes, but I never knew what it was...