Answer to rounding any number to a given decimal point

Michel Biedermann michelbi at oregon.uoregon.edu
Tue May 23 16:18:17 AEST 1989


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 at umigw.miami.edu).  Thanks Steve...
 
Michel Biedermann
U. of Oregon



More information about the Comp.lang.c mailing list