From 61264cfdc0daeae4690407cbb2045b65bcf8fe7f Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Fri, 29 May 2020 13:07:02 -0700 Subject: [PATCH] lib: isNegativeMixedAmount handles all-negative multi-comm. amounts --- hledger-lib/Hledger/Data/Amount.hs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/hledger-lib/Hledger/Data/Amount.hs b/hledger-lib/Hledger/Data/Amount.hs index c12a846c3..10bd2a148 100644 --- a/hledger-lib/Hledger/Data/Amount.hs +++ b/hledger-lib/Hledger/Data/Amount.hs @@ -609,11 +609,17 @@ averageMixedAmounts :: [MixedAmount] -> MixedAmount averageMixedAmounts [] = 0 averageMixedAmounts as = fromIntegral (length as) `divideMixedAmount` sum as --- | Is this mixed amount negative, if it can be normalised to a single commodity ? +-- | Is this mixed amount negative, if we can tell that unambiguously? +-- Ie when normalised, are all individual commodity amounts negative ? isNegativeMixedAmount :: MixedAmount -> Maybe Bool -isNegativeMixedAmount m = case as of [a] -> Just $ isNegativeAmount a - _ -> Nothing - where as = amounts $ normaliseMixedAmountSquashPricesForDisplay m +isNegativeMixedAmount m = + case amounts $ normaliseMixedAmountSquashPricesForDisplay m of + [] -> Just False + [a] -> Just $ isNegativeAmount a + as | all isNegativeAmount as -> Just True + _ -> Nothing -- multiple amounts with different signs + +-- XXX rename to mixedAmountLooksZero, mixedAmountIsZero, mixedAmountCostIsZero ? -- | Does this mixed amount appear to be zero when displayed with its given precision ? isZeroMixedAmount :: MixedAmount -> Bool