In a program, I calculate a percentage and print it to the user. The issue is that the percentage printed is shown with too many decimals. I searched for a function addressing this issue, didn't find one, and programmed the rounding function below, but I wonder if there is a more standard way to do this instead. Also, the idea described in this Java topic is pretty neat, and if there already are functions to do that, I would love to know.
roundTo :: (Integral int1, Integral int2) => int1 -> Double -> Either int2 Double
roundTo digitsAfterComma value
| (digitsAfterComma >= 1) =
let
factor = fromIntegral (10 ^ digitsAfterComma)
result = ((/ factor) . fromIntegral . round . (* factor)) value
in
Right result
| (digitsAfterComma == 0) = Left (round value)
| otherwise = error "minimal precision must be non-negative"
Update: A more complete version follows, developed thanks to some of the answers I've received.
import qualified Text.Printf as T
showPercentage :: (Integral a, Show a) => a -> Double -> String
showPercentage digitsAfterComma fraction
| (digitsAfterComma >= 1) =
let formatString = "%." ++ (show digitsAfterComma) ++ "f%%"
in T.printf formatString percentage
| (digitsAfterComma == 0) =
let formatString = "%d%%"
in T.printf formatString (round percentage :: Int)
| otherwise = error "minimal precision must be non-negative"
where percentage = 100 * fraction