diff --git a/hledger-lib/Hledger/Data/Amount.hs b/hledger-lib/Hledger/Data/Amount.hs index c4d2d63a7..9651541ba 100644 --- a/hledger-lib/Hledger/Data/Amount.hs +++ b/hledger-lib/Hledger/Data/Amount.hs @@ -72,6 +72,10 @@ module Hledger.Data.Amount ( maxprecisionwithpoint, setAmountPrecision, withPrecision, + setAmountInternalPrecision, + withInternalPrecision, + setAmountDecimalPoint, + withDecimalPoint, canonicaliseAmount, -- * MixedAmount nullmixedamt, @@ -261,6 +265,31 @@ showAmountDebug Amount{..} = printf "Amount {acommodity=%s, aquantity=%s, aprice showAmountWithoutPrice :: Amount -> String showAmountWithoutPrice a = showAmount a{aprice=NoPrice} +-- | Set an amount's internal precision, ie rounds the Decimal representing +-- the amount's quantity to some number of decimal places. +-- Rounding is done with Data.Decimal's default roundTo function: +-- "If the value ends in 5 then it is rounded to the nearest even value (Banker's Rounding)". +-- Does not change the amount's display precision. +-- Intended only for internal use, eg when comparing amounts in tests. +setAmountInternalPrecision :: Int -> Amount -> Amount +setAmountInternalPrecision p a@Amount{ aquantity=q, astyle=s } = a{ + astyle=s{asprecision=p} + ,aquantity=roundTo (fromIntegral p) q + } + +-- | Set an amount's internal precision, flipped. +-- Intended only for internal use, eg when comparing amounts in tests. +withInternalPrecision :: Amount -> Int -> Amount +withInternalPrecision = flip setAmountInternalPrecision + +-- | Set (or clear) an amount's display decimal point. +setAmountDecimalPoint :: Maybe Char -> Amount -> Amount +setAmountDecimalPoint mc a@Amount{ astyle=s } = a{ astyle=s{asdecimalpoint=mc} } + +-- | Set (or clear) an amount's display decimal point, flipped. +withDecimalPoint :: Amount -> Maybe Char -> Amount +withDecimalPoint = flip setAmountDecimalPoint + -- | Colour version. cshowAmountWithoutPrice :: Amount -> String cshowAmountWithoutPrice a = cshowAmount a{aprice=NoPrice}