--- /dev/null 1970-01-01 00:00:00.000000000 +0000 +++ b/internal/ledger/rounding.go 2026-03-04 11:02:19.000000000 +0000 @@ -0,0 +1,17 @@ +package ledger + +// HalfEven rounds a minor-unit amount to the nearest multiple of step using +// banker's rounding, which keeps long runs of postings from drifting upward. +func HalfEven(minor, step int64) int64 { + if step <= 1 { + return minor + } + q, r := minor/step, minor%step + switch { + case 2*r > step: + q++ + case 2*r == step && q%2 != 0: + q++ + } + return q * step +}