git-annex/Utility/Percentage.hs

34 lines
776 B
Haskell
Raw Normal View History

2012-04-29 21:48:07 +00:00
{- percentages
-
- Copyright 2012 Joey Hess <joey@kitenet.net>
-
- License: BSD-2-clause
2012-04-29 21:48:07 +00:00
-}
module Utility.Percentage (
Percentage,
percentage,
showPercentage
) where
import Data.Ratio
import Utility.HumanNumber
2012-04-29 21:48:07 +00:00
newtype Percentage = Percentage (Ratio Integer)
instance Show Percentage where
show = showPercentage 0
{- Normally the big number comes first. But 110% is allowed if desired. :) -}
percentage :: Integer -> Integer -> Percentage
percentage 0 _ = Percentage 0
percentage full have = Percentage $ have * 100 % full
{- Pretty-print a Percentage, with a specified level of precision. -}
showPercentage :: Int -> Percentage -> String
showPercentage precision (Percentage p) = v ++ "%"
2012-12-13 04:24:19 +00:00
where
v = showImprecise precision n
n = fromRational p :: Double