Skip to content

Commit d865ec5

Browse files
committed
lib: refactor: more consistent amount precision helpers
Hledger.Data.Amount: renamed: setAmountPrecision -> amountSetPrecision setFullPrecision -> amountSetFullPrecision setMixedAmountPrecision -> mixedAmountSetPrecision added: mixedAmountSetFullPrecision
1 parent d5ed809 commit d865ec5

File tree

7 files changed

+22
-16
lines changed

7 files changed

+22
-16
lines changed

hledger-lib/Hledger/Data/Amount.hs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ module Hledger.Data.Amount (
8383
showAmountWithZeroCommodity,
8484
showAmountDebug,
8585
showAmountWithoutPrice,
86-
setAmountPrecision,
86+
amountSetPrecision,
8787
withPrecision,
88-
setFullPrecision,
88+
amountSetFullPrecision,
8989
setAmountInternalPrecision,
9090
withInternalPrecision,
9191
setAmountDecimalPoint,
@@ -129,7 +129,8 @@ module Hledger.Data.Amount (
129129
showMixedAmountLinesB,
130130
wbToText,
131131
wbUnpack,
132-
setMixedAmountPrecision,
132+
mixedAmountSetPrecision,
133+
mixedAmountSetFullPrecision,
133134
canonicaliseMixedAmount,
134135
-- * misc.
135136
ltraceamount,
@@ -330,17 +331,17 @@ amountIsZero Amount{aquantity=q} = q == 0
330331

331332
-- | Set an amount's display precision, flipped.
332333
withPrecision :: Amount -> AmountPrecision -> Amount
333-
withPrecision = flip setAmountPrecision
334+
withPrecision = flip amountSetPrecision
334335

335336
-- | Set an amount's display precision.
336-
setAmountPrecision :: AmountPrecision -> Amount -> Amount
337-
setAmountPrecision p a@Amount{astyle=s} = a{astyle=s{asprecision=p}}
337+
amountSetPrecision :: AmountPrecision -> Amount -> Amount
338+
amountSetPrecision p a@Amount{astyle=s} = a{astyle=s{asprecision=p}}
338339

339340
-- | Increase an amount's display precision, if needed, to enough decimal places
340341
-- to show it exactly (showing all significant decimal digits, excluding trailing
341342
-- zeros).
342-
setFullPrecision :: Amount -> Amount
343-
setFullPrecision a = setAmountPrecision p a
343+
amountSetFullPrecision :: Amount -> Amount
344+
amountSetFullPrecision a = amountSetPrecision p a
344345
where
345346
p = max displayprecision naturalprecision
346347
displayprecision = asprecision $ astyle a
@@ -841,8 +842,13 @@ ltraceamount :: String -> MixedAmount -> MixedAmount
841842
ltraceamount s = traceWith (((s ++ ": ") ++).showMixedAmount)
842843

843844
-- | Set the display precision in the amount's commodities.
844-
setMixedAmountPrecision :: AmountPrecision -> MixedAmount -> MixedAmount
845-
setMixedAmountPrecision p = mapMixedAmount (setAmountPrecision p)
845+
mixedAmountSetPrecision :: AmountPrecision -> MixedAmount -> MixedAmount
846+
mixedAmountSetPrecision p = mapMixedAmount (amountSetPrecision p)
847+
848+
-- | In each component amount, increase the display precision sufficiently
849+
-- to render it exactly (showing all significant decimal digits).
850+
mixedAmountSetFullPrecision :: MixedAmount -> MixedAmount
851+
mixedAmountSetFullPrecision = mapMixedAmount amountSetFullPrecision
846852

847853
mixedAmountStripPrices :: MixedAmount -> MixedAmount
848854
mixedAmountStripPrices = mapMixedAmount (\a -> a{aprice=Nothing})

hledger-lib/Hledger/Data/Journal.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1278,7 +1278,7 @@ journalApplyAliases aliases j =
12781278
-- [ "P"
12791279
-- , showDate (pddate pd)
12801280
-- , T.unpack (pdcommodity pd)
1281-
-- , (showAmount . setAmountPrecision maxprecision) (pdamount pd
1281+
-- , (showAmount . amountSetPrecision maxprecision) (pdamount pd
12821282
-- )
12831283
-- ]
12841284

hledger-lib/Hledger/Read/TimedotReader.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ entryp = do
182182
tstatus = Cleared,
183183
tpostings = [
184184
nullposting{paccount=a
185-
,pamount=Mixed [setAmountPrecision (Precision 2) $ num hours] -- don't assume hours; do set precision to 2
185+
,pamount=Mixed [amountSetPrecision (Precision 2) $ num hours] -- don't assume hours; do set precision to 2
186186
,ptype=VirtualPosting
187187
,ptransaction=Just t
188188
}

hledger-ui/Hledger/UI/TransactionScreen.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ tsDraw UIState{aopts=UIOpts{cliopts_=copts@CliOpts{reportspec_=rspec@ReportSpec{
7272
_ -> [maincontent]
7373
where
7474
-- as with print, show amounts with all of their decimal places
75-
t = transactionMapPostingAmounts setFullPrecision t'
75+
t = transactionMapPostingAmounts amountSetFullPrecision t'
7676
maincontent = Widget Greedy Greedy $ do
7777
let
7878
prices = journalPriceOracle (infer_value_ ropts) j

hledger/Hledger/Cli/Commands/Add.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ amountAndCommentWizard PrevInput{..} EntryState{..} = do
373373
balancingamt = negate $ sum $ map pamount realps where realps = filter isReal esPostings
374374
balancingamtfirstcommodity = Mixed $ take 1 $ amounts balancingamt
375375
showamt =
376-
showMixedAmount . setMixedAmountPrecision
376+
showMixedAmount . mixedAmountSetPrecision
377377
-- what should this be ?
378378
-- 1 maxprecision (show all decimal places or none) ?
379379
-- 2 maxprecisionwithpoint (show all decimal places or .0 - avoids some but not all confusion with thousands separators) ?

hledger/Hledger/Cli/Commands/Close.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ close CliOpts{rawopts_=rawopts, reportspec_=rspec} j = do
9393

9494
-- since balance assertion amounts are required to be exact, the
9595
-- amounts in opening/closing transactions should be too (#941, #1137)
96-
precise = setFullPrecision
96+
precise = amountSetFullPrecision
9797

9898
-- interleave equity postings next to the corresponding closing posting, or put them all at the end ?
9999
interleaved = boolopt "interleaved" rawopts

hledger/Hledger/Cli/Commands/Print.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ print' opts j = do
6060
-- that. For now we try to reverse it by increasing all amounts' decimal places
6161
-- sufficiently to show the amount exactly. The displayed amounts may have minor
6262
-- differences from the originals, such as trailing zeroes added.
63-
let j' = journalMapPostingAmounts setFullPrecision j
63+
let j' = journalMapPostingAmounts amountSetFullPrecision j
6464
case maybestringopt "match" $ rawopts_ opts of
6565
Nothing -> printEntries opts j'
6666
Just desc -> printMatch opts j' $ T.pack desc

0 commit comments

Comments
 (0)