fix: show trailing decimal mark on cost amounts too, when needed

This commit is contained in:
Simon Michael 2024-05-01 14:16:53 -10:00
parent 81ec7c2c9d
commit 6796decb72
2 changed files with 30 additions and 28 deletions

View File

@ -87,7 +87,7 @@ module Hledger.Data.Amount (
showAmount, showAmount,
showAmountWith, showAmountWith,
showAmountB, showAmountB,
showAmountsCostB, showAmountCostB,
cshowAmount, cshowAmount,
showAmountWithZeroCommodity, showAmountWithZeroCommodity,
showAmountDebug, showAmountDebug,
@ -633,19 +633,6 @@ withDecimalPoint = flip setAmountDecimalPoint
-- Amount rendering -- Amount rendering
-- Show an amount's cost as @ UNITCOST or @@ TOTALCOST (builder version).
showAmountsCostB :: Amount -> WideBuilder
showAmountsCostB amt = case acost amt of
Nothing -> mempty
Just (UnitCost pa) -> WideBuilder (TB.fromString " @ ") 3 <> showAmountB defaultFmt{displayZeroCommodity=True} pa
Just (TotalCost pa) -> WideBuilder (TB.fromString " @@ ") 4 <> showAmountB defaultFmt{displayZeroCommodity=True} (sign pa)
where sign = if aquantity amt < 0 then negate else id
showAmountCostDebug :: Maybe AmountCost -> String
showAmountCostDebug Nothing = ""
showAmountCostDebug (Just (UnitCost pa)) = " @ " ++ showAmountDebug pa
showAmountCostDebug (Just (TotalCost pa)) = " @@ " ++ showAmountDebug pa
-- | Render an amount using its display style and the default amount format. -- | Render an amount using its display style and the default amount format.
-- Zero-equivalent amounts are shown as just \"0\". -- Zero-equivalent amounts are shown as just \"0\".
-- The special "missing" amount is shown as the empty string. -- The special "missing" amount is shown as the empty string.
@ -662,7 +649,7 @@ showAmountWith fmt = wbUnpack . showAmountB fmt
showAmountB :: AmountFormat -> Amount -> WideBuilder showAmountB :: AmountFormat -> Amount -> WideBuilder
showAmountB _ Amount{acommodity="AUTO"} = mempty showAmountB _ Amount{acommodity="AUTO"} = mempty
showAmountB showAmountB
AmountFormat{displayCommodity, displayZeroCommodity, displayDigitGroups afmt@AmountFormat{displayCommodity, displayZeroCommodity, displayDigitGroups
,displayForceDecimalMark, displayCost, displayColour} ,displayForceDecimalMark, displayCost, displayColour}
a@Amount{astyle=style} = a@Amount{astyle=style} =
color $ case ascommodityside style of color $ case ascommodityside style of
@ -676,7 +663,20 @@ showAmountB
| amountLooksZero a && not displayZeroCommodity = (WideBuilder (TB.singleton '0') 1, "") | amountLooksZero a && not displayZeroCommodity = (WideBuilder (TB.singleton '0') 1, "")
| otherwise = (quantity, quoteCommoditySymbolIfNeeded $ acommodity a) | otherwise = (quantity, quoteCommoditySymbolIfNeeded $ acommodity a)
space = if not (T.null comm) && ascommodityspaced style then WideBuilder (TB.singleton ' ') 1 else mempty space = if not (T.null comm) && ascommodityspaced style then WideBuilder (TB.singleton ' ') 1 else mempty
cost = if displayCost then showAmountsCostB a else mempty cost = if displayCost then showAmountCostB afmt a else mempty
-- Show an amount's cost as @ UNITCOST or @@ TOTALCOST (builder version).
showAmountCostB :: AmountFormat -> Amount -> WideBuilder
showAmountCostB afmt amt = case acost amt of
Nothing -> mempty
Just (UnitCost pa) -> WideBuilder (TB.fromString " @ ") 3 <> showAmountB afmt pa
Just (TotalCost pa) -> WideBuilder (TB.fromString " @@ ") 4 <> showAmountB afmt (sign pa)
where sign = if aquantity amt < 0 then negate else id
showAmountCostDebug :: Maybe AmountCost -> String
showAmountCostDebug Nothing = ""
showAmountCostDebug (Just (UnitCost pa)) = " @ " ++ showAmountDebug pa
showAmountCostDebug (Just (TotalCost pa)) = " @@ " ++ showAmountDebug pa
-- | Colour version. For a negative amount, adds ANSI codes to change the colour, -- | Colour version. For a negative amount, adds ANSI codes to change the colour,
-- currently to hard-coded red. -- currently to hard-coded red.

View File

@ -239,36 +239,38 @@ $ hledger -f- close -e 100000-01-01
> /99999-12-31 closing balances/ > /99999-12-31 closing balances/
>= >=
# ** 17. Not specific to close, but easy to reproduce with it: trailing decimal marks should be added # ** 17. close (and print) should add trailing decimal marks when needed to posting amounts and costs.
# in balance assertion/assignment amounts also, like posting amounts. (#2076)
< <
commodity $1,000.00
2000-01-01 2000-01-01
(assets:a) $1,000.00 (assets:a) $1,000 @@ $1,000
(assets:b) $1000
$ hledger -f- close --migrate -e 2001 $ hledger -f- close --migrate --show-costs -e 2001
2000-12-31 closing balances ; start: 2000-12-31 closing balances ; start:
assets:a $-1,000.00 = $0.00 assets:a $-1,000. @@ $1,000. = $0
assets:b $-1,000. = $0
equity:opening/closing balances equity:opening/closing balances
2001-01-01 opening balances ; start: 2001-01-01 opening balances ; start:
assets:a $1,000.00 = $1,000.00 assets:a $1,000. @@ $1,000. = $1,000.
assets:b $1,000. = $1,000.
equity:opening/closing balances equity:opening/closing balances
>= >=
# ** 18. close supports --round, like print. # ** 18. And to balance assertion and balance assignment amounts and costs. (#2076)
$ hledger -f- close --assert --show-costs -e 2001
2000-12-31 assert balances ; assert:
assets:a $0 = $1,000. @@ $1,000.
>=
# ** 19. close supports --round, like print.
$ hledger -f- close --migrate -e 2001 --round=hard -c '$1.0' $ hledger -f- close --migrate -e 2001 --round=hard -c '$1.0'
2000-12-31 closing balances ; start: 2000-12-31 closing balances ; start:
assets:a $-1000.0 = $0.0 assets:a $-1000.0 = $0.0
assets:b $-1000.0 = $0.0
equity:opening/closing balances equity:opening/closing balances
2001-01-01 opening balances ; start: 2001-01-01 opening balances ; start:
assets:a $1000.0 = $1000.0 assets:a $1000.0 = $1000.0
assets:b $1000.0 = $1000.0
equity:opening/closing balances equity:opening/closing balances
>= >=