diff options
author | Joey Hess <joey@kitenet.net> | 2013-07-19 19:39:14 -0400 |
---|---|---|
committer | Joey Hess <joey@kitenet.net> | 2013-07-19 19:39:14 -0400 |
commit | d3d9381f09fba70b0d622815b797358c6bcdc548 (patch) | |
tree | 38fcd594a788a56fc6f52552346f54f51ad3b2e1 /Utility/Percentage.hs | |
parent | 284970a49c77e8a410929231906766c538fe9269 (diff) |
refactor and unify code
This fixes several bugs in both modules.
Diffstat (limited to 'Utility/Percentage.hs')
-rw-r--r-- | Utility/Percentage.hs | 14 |
1 files changed, 5 insertions, 9 deletions
diff --git a/Utility/Percentage.hs b/Utility/Percentage.hs index 1c6b50062..8d9280148 100644 --- a/Utility/Percentage.hs +++ b/Utility/Percentage.hs @@ -13,6 +13,8 @@ module Utility.Percentage ( import Data.Ratio +import Utility.HumanNumber + newtype Percentage = Percentage (Ratio Integer) instance Show Percentage where @@ -25,14 +27,8 @@ 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) - | precision == 0 || remainder == 0 = go $ show int - | otherwise = go $ show int ++ "." ++ strip0s (show remainder) +showPercentage precision (Percentage p) = v ++ "%" where - go v = v ++ "%" - int :: Integer - (int, frac) = properFraction (fromRational p) - remainder = floor (frac * multiplier) :: Integer + v = showImprecise precision n + n = fromRational p :: Double strip0s = reverse . dropWhile (== '0') . reverse - multiplier :: Float - multiplier = 10 ** (fromIntegral precision) |