on master help sign in
Add percentage multiplication and division.
commitc506d9d43b72f1faffdeaffe143d6c40441e2dd0 authorNick <nick@typeable.io> dateMon 19 Dec 2022, 14:47 (4 years ago) parent37b155c
1 file changed+11
src/Data/Money/Internal.hs+11
src/Data/Money/Internal.hs+11
@@ -7,6 +7,8 @@
77module Data.Money.Internal ( Money(..)
88 , Currency
99 , KnownCurrency(..)
10+ , (*%)
11+ , (/%)
1012 ) where
1113
1214import Data.Fixed (Centi)
@@ -14,6 +16,7 @@ import Data.Group
1416import Data.Proxy
1517import GHC.Generics
1618import GHC.TypeLits
19+import GHC.Stack (HasCallStack)
1720
1821-- | Money type. This is actually 'Centi' from 'Data.Fixed' in disguise.
1922-- The type parameter is phantom, you probably want to use 'Currency' there.
@@ -55,3 +58,11 @@ instance KnownCurrency c => Read (Money c) where
5558 | otherwise = []
5659 where expCurr = currencyCode @c <> " "
5760 l = length expCurr
61+
62+(*%) :: Money curr -> Centi -> Money curr
63+(Money m) *% pct = Money (m * pct)
64+
65+(/%) :: HasCallStack => Money curr -> Centi -> Money curr
66+_ /% 0 = error "Called /% with 0 divisor"
67+(Money m) /% pct = Money (fromRational r)
68+ where r = toRational m / toRational pct