print: always show all decimal places (#931)

Ensures parseable and more sensible-looking output in more cases, and behaves more like Ledger's print.

There is still an issue with adding trailing zeroes, which would be nice to prevent.
This commit is contained in:
Simon Michael 2021-02-04 07:31:12 -08:00
parent 0274b30742
commit 6101368954
8 changed files with 37 additions and 13 deletions

View File

@ -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

View File

@ -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 =

View File

@ -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).

View File

@ -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
>=

View File

@ -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

View File

@ -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

View File

@ -27,7 +27,7 @@ hledger -f - print --explicit
2010-01-01
a 0
a 1C @ $1.0049
a $-1.00
a $-1.0049
>>>=0

View File

@ -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