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

View File

@ -1278,7 +1278,7 @@ journalApplyAliases aliases j =
-- [ "P"
-- , showDate (pddate 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,
tpostings = [
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
,ptransaction=Just t
}

View File

@ -72,7 +72,7 @@ tsDraw UIState{aopts=UIOpts{cliopts_=copts@CliOpts{reportspec_=rspec@ReportSpec{
_ -> [maincontent]
where
-- as with print, show amounts with all of their decimal places
t = transactionMapPostingAmounts setFullPrecision t'
t = transactionMapPostingAmounts amountSetFullPrecision t'
maincontent = Widget Greedy Greedy $ do
let
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
balancingamtfirstcommodity = Mixed $ take 1 $ amounts balancingamt
showamt =
showMixedAmount . setMixedAmountPrecision
showMixedAmount . mixedAmountSetPrecision
-- what should this be ?
-- 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) ?

View File

@ -93,7 +93,7 @@ close CliOpts{rawopts_=rawopts, reportspec_=rspec} j = do
-- since balance assertion amounts are required to be exact, the
-- 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 ?
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
-- sufficiently to show the amount exactly. The displayed amounts may have minor
-- 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
Nothing -> printEntries opts j'
Just desc -> printMatch opts j' $ T.pack desc