summaryrefslogtreecommitdiff
path: root/Utility/Percentage.hs
diff options
context:
space:
mode:
authorSean Whitton <spwhitton@spwhitton.name>2020-01-02 18:07:10 +0000
committerSean Whitton <spwhitton@spwhitton.name>2020-01-02 18:07:10 +0000
commit1092316ba04116d7ae1d4cdf347804feacef768a (patch)
treefbc819794d4202a4ac778f0f6be57eedef7518dd /Utility/Percentage.hs
parentd1e0531dd8e8b842349421a898b74b212d2157e8 (diff)
parent50a5a7b101cc9c30a5a04b4c20cbf7f99fcab0ef (diff)
downloadgit-repair-1092316ba04116d7ae1d4cdf347804feacef768a.tar.gz
Merge tag '1.20200102'
tagging package git-repair version 1.20200102
Diffstat (limited to 'Utility/Percentage.hs')
-rw-r--r--Utility/Percentage.hs33
1 files changed, 33 insertions, 0 deletions
diff --git a/Utility/Percentage.hs b/Utility/Percentage.hs
new file mode 100644
index 0000000..a30c260
--- /dev/null
+++ b/Utility/Percentage.hs
@@ -0,0 +1,33 @@
+{- percentages
+ -
+ - Copyright 2012 Joey Hess <id@joeyh.name>
+ -
+ - License: BSD-2-clause
+ -}
+
+module Utility.Percentage (
+ Percentage,
+ percentage,
+ showPercentage
+) where
+
+import Data.Ratio
+
+import Utility.HumanNumber
+
+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 ++ "%"
+ where
+ v = showImprecise precision n
+ n = fromRational p :: Double