lib: refactor: more consistent amount precision helpers

Hledger.Data.Amount:
renamed:
setAmountPrecision -> amountSetPrecision
setFullPrecision -> amountSetFullPrecision
setMixedAmountPrecision -> mixedAmountSetPrecision
added:
mixedAmountSetFullPrecision
This commit is contained in:
Simon Michael 2021-02-04 13:57:55 -08:00
parent d5ed8090fa
commit d865ec5d65
7 changed files with 22 additions and 16 deletions

View File

@ -83,9 +83,9 @@ module Hledger.Data.Amount (
showAmountWithZeroCommodity, showAmountWithZeroCommodity,
showAmountDebug, showAmountDebug,
showAmountWithoutPrice, showAmountWithoutPrice,
setAmountPrecision, amountSetPrecision,
withPrecision, withPrecision,
setFullPrecision, amountSetFullPrecision,
setAmountInternalPrecision, setAmountInternalPrecision,
withInternalPrecision, withInternalPrecision,
setAmountDecimalPoint, setAmountDecimalPoint,
@ -129,7 +129,8 @@ module Hledger.Data.Amount (
showMixedAmountLinesB, showMixedAmountLinesB,
wbToText, wbToText,
wbUnpack, wbUnpack,
setMixedAmountPrecision, mixedAmountSetPrecision,
mixedAmountSetFullPrecision,
canonicaliseMixedAmount, canonicaliseMixedAmount,
-- * misc. -- * misc.
ltraceamount, ltraceamount,
@ -330,17 +331,17 @@ amountIsZero Amount{aquantity=q} = q == 0
-- | Set an amount's display precision, flipped. -- | Set an amount's display precision, flipped.
withPrecision :: Amount -> AmountPrecision -> Amount withPrecision :: Amount -> AmountPrecision -> Amount
withPrecision = flip setAmountPrecision withPrecision = flip amountSetPrecision
-- | Set an amount's display precision. -- | Set an amount's display precision.
setAmountPrecision :: AmountPrecision -> Amount -> Amount amountSetPrecision :: AmountPrecision -> Amount -> Amount
setAmountPrecision p a@Amount{astyle=s} = a{astyle=s{asprecision=p}} amountSetPrecision p a@Amount{astyle=s} = a{astyle=s{asprecision=p}}
-- | Increase an amount's display precision, if needed, to enough decimal places -- | Increase an amount's display precision, if needed, to enough decimal places
-- to show it exactly (showing all significant decimal digits, excluding trailing -- to show it exactly (showing all significant decimal digits, excluding trailing
-- zeros). -- zeros).
setFullPrecision :: Amount -> Amount amountSetFullPrecision :: Amount -> Amount
setFullPrecision a = setAmountPrecision p a amountSetFullPrecision a = amountSetPrecision p a
where where
p = max displayprecision naturalprecision p = max displayprecision naturalprecision
displayprecision = asprecision $ astyle a displayprecision = asprecision $ astyle a
@ -841,8 +842,13 @@ ltraceamount :: String -> MixedAmount -> MixedAmount
ltraceamount s = traceWith (((s ++ ": ") ++).showMixedAmount) ltraceamount s = traceWith (((s ++ ": ") ++).showMixedAmount)
-- | Set the display precision in the amount's commodities. -- | Set the display precision in the amount's commodities.
setMixedAmountPrecision :: AmountPrecision -> MixedAmount -> MixedAmount mixedAmountSetPrecision :: AmountPrecision -> MixedAmount -> MixedAmount
setMixedAmountPrecision p = mapMixedAmount (setAmountPrecision p) mixedAmountSetPrecision p = mapMixedAmount (amountSetPrecision p)
-- | In each component amount, increase the display precision sufficiently
-- to render it exactly (showing all significant decimal digits).
mixedAmountSetFullPrecision :: MixedAmount -> MixedAmount
mixedAmountSetFullPrecision = mapMixedAmount amountSetFullPrecision
mixedAmountStripPrices :: MixedAmount -> MixedAmount mixedAmountStripPrices :: MixedAmount -> MixedAmount
mixedAmountStripPrices = mapMixedAmount (\a -> a{aprice=Nothing}) mixedAmountStripPrices = mapMixedAmount (\a -> a{aprice=Nothing})

View File

@ -1278,7 +1278,7 @@ journalApplyAliases aliases j =
-- [ "P" -- [ "P"
-- , showDate (pddate pd) -- , showDate (pddate pd)
-- , T.unpack (pdcommodity pd) -- , T.unpack (pdcommodity pd)
-- , (showAmount . setAmountPrecision maxprecision) (pdamount pd -- , (showAmount . amountSetPrecision maxprecision) (pdamount pd
-- ) -- )
-- ] -- ]

View File

@ -182,7 +182,7 @@ entryp = do
tstatus = Cleared, tstatus = Cleared,
tpostings = [ tpostings = [
nullposting{paccount=a nullposting{paccount=a
,pamount=Mixed [setAmountPrecision (Precision 2) $ num hours] -- don't assume hours; do set precision to 2 ,pamount=Mixed [amountSetPrecision (Precision 2) $ num hours] -- don't assume hours; do set precision to 2
,ptype=VirtualPosting ,ptype=VirtualPosting
,ptransaction=Just t ,ptransaction=Just t
} }

View File

@ -72,7 +72,7 @@ tsDraw UIState{aopts=UIOpts{cliopts_=copts@CliOpts{reportspec_=rspec@ReportSpec{
_ -> [maincontent] _ -> [maincontent]
where where
-- as with print, show amounts with all of their decimal places -- as with print, show amounts with all of their decimal places
t = transactionMapPostingAmounts setFullPrecision t' t = transactionMapPostingAmounts amountSetFullPrecision t'
maincontent = Widget Greedy Greedy $ do maincontent = Widget Greedy Greedy $ do
let let
prices = journalPriceOracle (infer_value_ ropts) j prices = journalPriceOracle (infer_value_ ropts) j

View File

@ -373,7 +373,7 @@ amountAndCommentWizard PrevInput{..} EntryState{..} = do
balancingamt = negate $ sum $ map pamount realps where realps = filter isReal esPostings balancingamt = negate $ sum $ map pamount realps where realps = filter isReal esPostings
balancingamtfirstcommodity = Mixed $ take 1 $ amounts balancingamt balancingamtfirstcommodity = Mixed $ take 1 $ amounts balancingamt
showamt = showamt =
showMixedAmount . setMixedAmountPrecision showMixedAmount . mixedAmountSetPrecision
-- what should this be ? -- what should this be ?
-- 1 maxprecision (show all decimal places or none) ? -- 1 maxprecision (show all decimal places or none) ?
-- 2 maxprecisionwithpoint (show all decimal places or .0 - avoids some but not all confusion with thousands separators) ? -- 2 maxprecisionwithpoint (show all decimal places or .0 - avoids some but not all confusion with thousands separators) ?

View File

@ -93,7 +93,7 @@ close CliOpts{rawopts_=rawopts, reportspec_=rspec} j = do
-- since balance assertion amounts are required to be exact, the -- since balance assertion amounts are required to be exact, the
-- amounts in opening/closing transactions should be too (#941, #1137) -- amounts in opening/closing transactions should be too (#941, #1137)
precise = setFullPrecision precise = amountSetFullPrecision
-- interleave equity postings next to the corresponding closing posting, or put them all at the end ? -- interleave equity postings next to the corresponding closing posting, or put them all at the end ?
interleaved = boolopt "interleaved" rawopts interleaved = boolopt "interleaved" rawopts

View File

@ -60,7 +60,7 @@ print' opts j = do
-- that. For now we try to reverse it by increasing all amounts' decimal places -- that. For now we try to reverse it by increasing all amounts' decimal places
-- sufficiently to show the amount exactly. The displayed amounts may have minor -- sufficiently to show the amount exactly. The displayed amounts may have minor
-- differences from the originals, such as trailing zeroes added. -- differences from the originals, such as trailing zeroes added.
let j' = journalMapPostingAmounts setFullPrecision j let j' = journalMapPostingAmounts amountSetFullPrecision j
case maybestringopt "match" $ rawopts_ opts of case maybestringopt "match" $ rawopts_ opts of
Nothing -> printEntries opts j' Nothing -> printEntries opts j'
Just desc -> printMatch opts j' $ T.pack desc Just desc -> printMatch opts j' $ T.pack desc