[comp.lang.c] Answer to rounding any number to a given decimal point

michelbi@oregon.uoregon.edu (Michel Biedermann) (05/23/89)

Here is the most elegant answer to the problem of rounding-off an arbitrary
number to an arbitrary decimal level.
 
#define round(x,y) ((x < 0.0F?  ceil((x)/(y)-.5F)*(y) : floor((x)/(y)+.5F)*(y)))

round(1.5555,.1)	will return 1.6
round(-1.555,.1)	will return -1.6
round(.502,.01)		will return .50

Credit is due to Steve Emmerson (steve@umigw.miami.edu).  Thanks Steve...
 
Michel Biedermann
U. of Oregon