diff --git a/hledger-lib/Hledger/Data/Journal.hs b/hledger-lib/Hledger/Data/Journal.hs index 48b317540..710be6859 100644 --- a/hledger-lib/Hledger/Data/Journal.hs +++ b/hledger-lib/Hledger/Data/Journal.hs @@ -41,6 +41,7 @@ module Hledger.Data.Journal ( mapJournalTransactions, mapJournalPostings, mapTransactionPostings, + journalMapPostingAmounts, -- * Querying journalAccountNamesUsed, journalAccountNamesImplied, @@ -455,6 +456,10 @@ mapJournalPostings f j@Journal{jtxns=ts} = j{jtxns=map (mapTransactionPostings f mapTransactionPostings :: (Posting -> Posting) -> Transaction -> Transaction mapTransactionPostings f t@Transaction{tpostings=ps} = t{tpostings=map f ps} +-- | Apply a transformation to a journal's posting amounts. +journalMapPostingAmounts :: (Amount -> Amount) -> Journal -> Journal +journalMapPostingAmounts f = mapJournalPostings (postingTransformAmount (mapMixedAmount f)) + {- ------------------------------------------------------------------------------- -- filtering V1 diff --git a/hledger/Hledger/Cli/Commands/Print.hs b/hledger/Hledger/Cli/Commands/Print.hs index 5fbcf5eaa..0e96d370b 100644 --- a/hledger/Hledger/Cli/Commands/Print.hs +++ b/hledger/Hledger/Cli/Commands/Print.hs @@ -54,9 +54,16 @@ printmode = hledgerCommandMode -- | Print journal transactions in standard format. print' :: CliOpts -> Journal -> IO () print' opts j = do + -- The print command should show all amounts with their original decimal places, + -- but as part of journal reading the posting amounts have already been normalised + -- according to commodity display styles, and currently it's not easy to avoid + -- 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 case maybestringopt "match" $ rawopts_ opts of - Nothing -> printEntries opts j - Just desc -> printMatch opts j $ T.pack desc + Nothing -> printEntries opts j' + Just desc -> printMatch opts j' $ T.pack desc printEntries :: CliOpts -> Journal -> IO () printEntries opts@CliOpts{reportspec_=rspec} j = diff --git a/hledger/Hledger/Cli/Commands/Print.md b/hledger/Hledger/Cli/Commands/Print.md index b9f168962..c8948a685 100644 --- a/hledger/Hledger/Cli/Commands/Print.md +++ b/hledger/Hledger/Cli/Commands/Print.md @@ -6,8 +6,20 @@ _FLAGS The print command displays full journal entries (transactions) from the journal file, sorted by date (or with `--date2`, by [secondary date](#secondary-dates)). + +Amounts are shown mostly normalised to +[commodity display style](#commodity-display-styles), +eg the placement of commodity symbols will be consistent. +All of their decimal places are shown, as in the original journal entry +(with one alteration: in some cases trailing zeroes are added.) + Amounts are shown right-aligned within each transaction (but not across all transactions). -Directives and inter-transaction comments are not shown. + +Directives and inter-transaction comments are not shown, currently. +This means the print command is somewhat lossy, and if you are using it to +reformat your journal you should take care to also copy over the directives +and file-level comments. + Eg: ```shell @@ -44,7 +56,6 @@ $ hledger print assets:cash | hledger -f- -I reg expenses:food There are some situations where print's output can become unparseable: -- [Rounding](#rounding) amounts according to [commodity display styles](#commodity-display-style) can cause transactions to appear [unbalanced](https://github.com/simonmichael/hledger/issues/931). - [Valuation](#valuation) affects posting amounts but not [balance assertion](#balance-assertions) or [balance assignment](#balance-assignments) amounts, potentially causing those to [fail](https://github.com/simonmichael/hledger/issues/1429). - [Auto postings](#auto-postings) can generate postings with [too many missing amounts](https://github.com/simonmichael/hledger/issues/1276). diff --git a/hledger/test/journal/amounts-and-commodities.test b/hledger/test/journal/amounts-and-commodities.test index add90b0e3..1301630ce 100644 --- a/hledger/test/journal/amounts-and-commodities.test +++ b/hledger/test/journal/amounts-and-commodities.test @@ -196,7 +196,7 @@ $ hledger -f - print >= -# Balance assertion amounts are always displayed at their full precision, +# 14. Balance assertion amounts are always displayed at their full precision, # overriding commodity styles. (#1465) < commodity A 1. @@ -206,7 +206,7 @@ commodity A 1. (a) 1.122A = 1.123A $ hledger -f - print 2021-01-01 - (a) 0 - (a) A 1 = A 1.123 + (a) A 0.001 + (a) A 1.122 = A 1.123 >= diff --git a/hledger/test/journal/auto-postings.test b/hledger/test/journal/auto-postings.test index 8cf676b28..f8cb27aaf 100644 --- a/hledger/test/journal/auto-postings.test +++ b/hledger/test/journal/auto-postings.test @@ -17,7 +17,7 @@ $ hledger print -f- --auto income:remuneration $-100 (liabilities:tax) $-33 ; income tax, generated-posting: = ^income income:donations $-15 - (liabilities:tax) $-5 ; income tax, generated-posting: = ^income + (liabilities:tax) $-4.95 ; income tax, generated-posting: = ^income assets:bank 2016-01-01 withdraw @@ -266,7 +266,7 @@ $ hledger print -f- --auto --forecast -b 2016-01 -e 2016-03 income:remuneration $-100 (liabilities:tax) $-33 ; income tax, generated-posting: = ^income income:donations $-15 - (liabilities:tax) $-5 ; income tax, generated-posting: = ^income + (liabilities:tax) $-4.95 ; income tax, generated-posting: = ^income assets:bank >= @@ -339,6 +339,6 @@ commodity 0. B $ hledger -f- print --auto 2020-01-20 ; modified: (a) 1 A - (b) 2 B ; generated-posting: = + (b) 1.5 B ; generated-posting: = >=0 diff --git a/hledger/test/journal/default-commodity.test b/hledger/test/journal/default-commodity.test index b3dab64b6..40a718d0a 100644 --- a/hledger/test/journal/default-commodity.test +++ b/hledger/test/journal/default-commodity.test @@ -65,7 +65,7 @@ D 1,000.0 A $ hledger -f- print 2020-01-01 - (a) 1,000.1 A + (a) 1,000.123 A >=0 diff --git a/hledger/test/journal/precision.test b/hledger/test/journal/precision.test index a28547440..18ced3ac3 100644 --- a/hledger/test/journal/precision.test +++ b/hledger/test/journal/precision.test @@ -27,7 +27,7 @@ hledger -f - print --explicit 2010-01-01 a 0 a 1C @ $1.0049 - a $-1.00 + a $-1.0049 >>>=0 diff --git a/hledger/test/rewrite.test b/hledger/test/rewrite.test index 66a913eea..66260bcc8 100644 --- a/hledger/test/rewrite.test +++ b/hledger/test/rewrite.test @@ -1,3 +1,4 @@ + # 1. Add proportional income tax (from documentation) hledger rewrite -f- ^income --add-posting '(liabilities:tax) *.33 ; income tax' <<< @@ -14,7 +15,7 @@ hledger rewrite -f- ^income --add-posting '(liabilities:tax) *.33 ; income tax income:remuneration $-100 (liabilities:tax) $-33 ; income tax, generated-posting: = ^income income:donations $-15 - (liabilities:tax) $-5 ; income tax, generated-posting: = ^income + (liabilities:tax) $-4.95 ; income tax, generated-posting: = ^income assets:bank 2016-01-01 withdraw