Visitar URL original
print can show unbalanced entries due to display rounding · Issue #931 · hledgerorg/hledger · GitHub
Skip to content

print can show unbalanced entries due to display rounding #931

Closed

Description

@simonmichael

This frequently annoys me when I need to re-parse print's output, eg when doing a query that requires a pipeline. Time to do something about it.

; limit display precision to 2 places
commodity $0.00

1/1
   a  -$0.01
   b   $0.005
   c   $0.005

print shows an unbalanced transaction:

$ hledger print
2018/01/01
    a          $-0.01
    b               0
    c               0

which can not be re-parsed:

$ hledger print | hledger -f- print
hledger: "-" (lines 1-4)
could not balance this transaction (real postings are off by $-0.01)
2018/01/01
    a          $-0.01
    b               0
    c               0


Activity

  1. added
    A-BUGSomething wrong, confusing or sub-standard in the software, docs, or user experience.
    on Nov 29, 2018
  2. simonmichael commented on Nov 29, 2018

    @simonmichael
    MemberAuthor

    PS, a use case to justify this: fractions of a cent don't typically arise in day to day transactions, but they do arise eg when splitting shared expenses according to some rule, eg splitting an odd amount 50-50.

    You can work around it by putting the extra cent on one amount or the other, but this requires thought and manual intervention and can cause disagreement with external systems - I would rather use the correct mathematical split.

    You can also work around it by temporarily commenting out the commodity directive to allow unlimited display precision. But this is manual and annoying (and now all dollar amounts are rendered with three decimal places).

  3. pinned this issue on Dec 6, 2019
  4. unpinned this issue on Dec 6, 2019
  5. simonmichael commented on Oct 23, 2020

    @simonmichael
    MemberAuthor

    See also @rhyanki's example on #1276. Rounding according to the commodity display styles can cause invalid/unparseable journal entries to be printed.

    What is better behaviour here ? I guess print should be allowed to print more decimal places when needed to guarantee a valid entry, overriding the display styles ? I'm not sure what the algorithm would be exactly.

  6. changed the title [-]print can show unbalanced transactions with a commodity directive[/-] [+]print can show unbalanced entries due to display rounding[/+] on Oct 23, 2020
  7. rhyanki commented on Oct 24, 2020

    @rhyanki

    I guess the algorithm would be to iteratively increment the number of decimal places until it balances? Intuitively that seems like it should be guaranteed to find a solution, but I'm not certain.

    In the case of auto-transactions, this approach could lead to very ugly print output, for a potentially very high percentage of transactions in the journal (not so much an issue with the commodity directive example in the OP).

    Another approach might be to let the user specify a special "rounding" account (or pick one by default). Then for any transaction which is unbalanced in the printed output, we add a posting to the rounding account which balances it out. So for my example, the new output might be:

    $ hledger print --auto
    2020-01-01 Test  ; modified:
        A           $ 0.03
        A1          $ 0.02
        A2          $ 0.02
        A3         $ -0.03
        B          $ -0.03
        Rounding   $ -0.01 ; this one gets generated as part of the print command
    

    Then we don't need to print more decimal places, and any transaction which is already balanced in the output can still be left alone.

  8. simonmichael commented on Feb 4, 2021

    @simonmichael
    MemberAuthor

    Another approach: have the print command ignore the "decimal places" part of commodity styles, ie always show the original number of decimal places for all amounts.

  9. simonmichael commented on Feb 4, 2021

    @simonmichael
    MemberAuthor

    Which actually is what Ledger's print command does, mostly (it strips trailing zeroes).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-BUGSomething wrong, confusing or sub-standard in the software, docs, or user experience.print

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions