Skip to content

Commit

Permalink
fix: print: don't round in implicit conversions (fix #2079)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonmichael committed Sep 2, 2023
1 parent fd97c40 commit 4bd0dd7
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 3 deletions.
5 changes: 3 additions & 2 deletions hledger-lib/Hledger/Data/Journal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -793,13 +793,14 @@ journalModifyTransactions verbosetags d j =
Left err -> Left err

-- | Choose and apply a consistent display style to the posting
-- amounts in each commodity (see journalCommodityStyles).
-- amounts in each commodity (see journalCommodityStyles),
-- keeping all display precisions unchanged.
-- Can return an error message eg if inconsistent number formats are found.
journalApplyCommodityStyles :: Journal -> Either String Journal
journalApplyCommodityStyles = fmap fixjournal . journalInferCommodityStyles
where
fixjournal j@Journal{jpricedirectives=pds} =
journalMapPostings (postingApplyCommodityStyles styles) j{jpricedirectives=map fixpricedirective pds}
journalMapPostings (postingApplyCommodityStylesExceptPrecision styles) j{jpricedirectives=map fixpricedirective pds}
where
styles = journalCommodityStyles j
fixpricedirective pd@PriceDirective{pdamount=a} = pd{pdamount=amountSetStylesExceptPrecision styles a}
Expand Down
2 changes: 1 addition & 1 deletion hledger/Hledger/Cli/Commands/Print.hs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ printEntries opts@CliOpts{reportspec_=rspec} j =
styleAmounts styles $
entriesReport rspec j
where
styles = journalCommodityStyles j
styles = M.map amountStyleUnsetPrecision $ journalCommodityStyles j -- keep all precisions unchanged
fmt = outputFormatFromOpts opts
render | fmt=="txt" = entriesReportAsText opts
| fmt=="csv" = printCSV . entriesReportAsCsv
Expand Down
75 changes: 75 additions & 0 deletions hledger/test/print/print-style.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# print amount styling tests
#
# The amounts are:
# amt - the posting amount
# cost - the posting amount's cost
# bal - the balance assertion amount
# balcost - the balance assertion amount's cost
#
# Styling includes:
# basic - everything except precision
# prec - precision (number of decimal places)
#
# Historical behaviour:
# | hledger | amt basic | cost basic | bal basic | balcost basic | amt prec | cost prec | bal prec | balcost prec |
# | 1.14 | Y | N | N | N | Y | N | N | N |
# | 1.22 | Y | N | Y | N | N | N | N | N |
# | 1.30 | Y | Y | Y | N | N | N | N | N |
# | 1.31 | Y | Y | Y | Y | N | N | N | N |
#
# In the following,
# basic styling will move the commodity symbol to the left
# precision styling will hide the decimal place

# 1. print styles all amounts, but leaves all precisions unchanged, even with -c.
<
commodity A 1000.
commodity B 1000.

2023-01-01
(a) 1.2 A @ 3.4 B = 1.2 A @ 3.4 B

$ hledger -f- print -c A1000.00 -c B1000.00
2023-01-01
(a) A1.2 @ B3.4 = A1.2 @ B3.4

>=

# 2. Precisions are also preserved when there's an implicit conversion (#2079).
<
commodity A 1000.

2023-01-01
f A 1.5
g A 1.5
c B -3

$ hledger -f- print
2023-01-01
f A 1.5
g A 1.5
c B -3

>=


# Maybe later:

# # 0. With print, -c has extra power: it can increase all amount precisions.
# $ hledger -f- print -c A1000.00 -c B1000.00
# 2023-01-01
# (a) A1.20 @ B3.40 = A1.20 @ B3.40
#
# >=
#
# # 0. And -c can decrease trailing decimal zeros. But not significant decimal digits
# (because that would change transactions).
# <
# 2023-01-01
# (a) 1.2340 A @ 3.4560 B = 1.234 A @ 3.456 B
#
# $ hledger -f- print -c A1000. -c B1000.
# 2023-01-01
# (a) A1.234 @ B3.456 = A1.234 @ B3.456
#
# >=

0 comments on commit 4bd0dd7

Please sign in to comment.