2012-04-29 21:48:07 +00:00
|
|
|
{- percentages
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2012 Joey Hess <id@joeyh.name>
|
2012-04-29 21:48:07 +00:00
|
|
|
-
|
2014-05-10 14:01:27 +00:00
|
|
|
- License: BSD-2-clause
|
2012-04-29 21:48:07 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
module Utility.Percentage (
|
|
|
|
Percentage,
|
|
|
|
percentage,
|
|
|
|
showPercentage
|
|
|
|
) where
|
|
|
|
|
|
|
|
import Data.Ratio
|
|
|
|
|
2013-07-19 23:39:14 +00:00
|
|
|
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
|
2013-07-19 23:39:14 +00:00
|
|
|
showPercentage precision (Percentage p) = v ++ "%"
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
2013-07-19 23:39:14 +00:00
|
|
|
v = showImprecise precision n
|
|
|
|
n = fromRational p :: Double
|