From c42496b1341b4ccb546d1e579ef18d2b5e7d10d8 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Tue, 27 Jul 2010 23:20:20 +0000 Subject: [PATCH] sign testing for amounts --- hledger-lib/Hledger/Data/Amount.hs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/hledger-lib/Hledger/Data/Amount.hs b/hledger-lib/Hledger/Data/Amount.hs index adfd26e6d..8cc6170da 100644 --- a/hledger-lib/Hledger/Data/Amount.hs +++ b/hledger-lib/Hledger/Data/Amount.hs @@ -153,6 +153,10 @@ isReallyZeroAmount :: Amount -> Bool isReallyZeroAmount = null . filter (`elem` "123456789") . printf ("%."++show zeroprecision++"f") . quantity where zeroprecision = 8 +-- | Is this amount negative ? The price is ignored. +isNegativeAmount :: Amount -> Bool +isNegativeAmount Amount{quantity=q} = q < 0 + -- | Access a mixed amount's components. amounts :: MixedAmount -> [Amount] amounts (Mixed as) = as @@ -166,6 +170,13 @@ isZeroMixedAmount = all isZeroAmount . amounts . normaliseMixedAmount isReallyZeroMixedAmount :: MixedAmount -> Bool isReallyZeroMixedAmount = all isReallyZeroAmount . amounts . normaliseMixedAmount +-- | Is this mixed amount negative, if it can be normalised to a single commodity ? +isNegativeMixedAmount :: MixedAmount -> Maybe Bool +isNegativeMixedAmount m = case as of [a] -> Just $ isNegativeAmount a + _ -> Nothing + where + as = amounts $ normaliseMixedAmount m + -- | Is this mixed amount "really" zero, after converting to cost -- commodities where possible ? isReallyZeroMixedAmountCost :: MixedAmount -> Bool