Skip to content

feat: support arithmetic expressions in posting amounts - #2726

Closed
0Xgit wants to merge 2 commits into
hledgerorg:mainfrom
0Xgit:amount-arithmetic
Closed

0Xgit wants to merge 2 commits into
hledgerorg:mainfrom
0Xgit:amount-arithmetic

Conversation

@0Xgit

@0Xgit 0Xgit commented Sep 11, 2026

Copy link
Copy Markdown

PR: support arithmetic expressions in posting amounts

Fixes #2648.

What this does

Posting amounts can now contain simple arithmetic, so the parts stay visible
in the journal instead of being hand-added first:

2026-01-01 shop
  expenses:groceries   $21.60 + $27.68
  assets:cash

Supported: + - * / with normal precedence, and parentheses.
$21.60 + $27.68 prints as $49.28.
The issue's example works: $-50.20 + $-209.00 + $-16.54 + $-438 gives $-713.74.
Tax-style scaling works too: (1 + 0.05) * $47.97 gives $50.3685.

Rules

  • + and - need the same commodity on both sides. A bare number takes the
    other side's commodity, so $5 + 3 is $8.
  • * and / need a plain number on the right. * also allows a bare number
    (or bracketed expression) on the left, so (1 + 0.05) * $47.97 is valid.
  • Anything else (mixed commodities, divide by zero) is a parse error pointing
    at the bad region.
  • The kept operand's display style is reused; display precision follows the
    result ($4.20 * 2 stays $8.40). Division is computed with 6 extra places,
    then trailing zeroes are trimmed.
  • ) is no longer accepted as an unquoted commodity character, so bracketed
    expressions parse. Use ")" quoted if a symbol really needs it.

Files

  • hledger-lib/Hledger/Read/Common.hs — expression parser (simpleamountp
    now parses additive/multiplicative/primary levels), applyArithOp, tests.
  • hledger-lib/Hledger/Data/Amount.hs — one-char change to the commodity list.
  • hledger/hledger.m4.md — short docs section.

Testing

  • cabal run hledger-lib:unittest -- -p 'amountp': 14/14 pass (9 new cases).
  • Full cabal run hledger-lib:unittest: 249/249 pass.
  • Built the hledger binary and ran print on the examples above; output
    matches, and the balanced postings still balance.
  • Functional tests: moved case 5 ((1 + 1) amount) out of
    ledger-compat/hledger-unsupported.test (it asserted failure, which this
    feature intentionally changes) into journal/amounts-and-commodities.test
    as case 28, asserting the new print output. Both files pass locally
    (32/32 via shelltest).

AI disclosure (required by hledger.org/AI.html rule 4)

AI assistance: yes. Provider/model: Muse Spark (Meta, agentic coding in
opencode). Rough output: on the order of tens of thousands of tokens across
debugging and test-fix iterations. I reviewed, tested, and take responsibility
for every line (rule 7). Note: this would be my first hledger PR — flagging
per rule 6; happy to rework or hand-rewrite anything the maintainer prefers.
No OpenAI models used (rule 5).

@0Xgit 0Xgit closed this Sep 11, 2026
@0Xgit
0Xgit deleted the amount-arithmetic branch September 11, 2026 08:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support simple arithmetic expressions in posting amounts

1 participant