I am trying to solve a simple problem - rounding a number to a nearest multiple of a given float. As an example, I would like
roundmult(3811.47123123, 0.01) = 3811.47. However, the simplest implementation of
roundmult(val, prec) = round(val / prec) * prec backfires by outputting
roundmult(3811.47123123, 0.01) = 3811.4700000000003.
Is there any simple way of achieving this using some built-in functionality, or some magic of rounding the above result once more to the precision given by
prec? Something like
roundmult(val, prec) = round(round(val / prec) * prec, digits=ceil(Integer, -log10(prec)+1)) would probably work, but it is so ugly.