Skip to content

Instantly share code, notes, and snippets.

@cmoore
Created May 19, 2011 05:13
Show Gist options
  • Save cmoore/980232 to your computer and use it in GitHub Desktop.
Save cmoore/980232 to your computer and use it in GitHub Desktop.
Sanely rounding a number.
roundp :: (RealFrac a1, Integral a, Floating a1) => a1 -> a -> a1
roundp n s = fromIntegral (round (n * factor)) / factor
where
shift = s - (floor (logBase 10 n) + 1)
factor = 10 ** fromIntegral shift
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment