const MaxCents
MaxCents bounds one amount: 100 million units of a currency. With rates up to MaxRate the products below stay far inside int64.
Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.
Package ledger is gnosplit's arithmetic: amounts in cents, how an expense is split, and the fewest transfers that set...
Package ledger is gnosplit's arithmetic: amounts in cents, how an expense is split, and the fewest transfers that settle a group. It holds no state, so a later gnosplit release and an app can reuse it as is.
Convert turns cents into cents of another currency at rate (millionths), rounding half up. It splits the rate so no product overflows.
Equal splits total into n parts that differ by at most one cent; the first total%n parts take the extra cent. A negative total (money received) splits the same way, every part negative.
Format writes cents as "1,234.56", "-12.00" or "0.00".
ParseAmount reads an amount in the group's currency ("42.50"), or a foreign one with its rate ("50 USD x0.92": 50 dollars at 0.92 each). note is the foreign amount as typed, cleaned up, and "" for a plain one.
ParseCents reads a positive amount written "42", "42.5" or "42,50": digits, then at most one "." or "," and two decimals. Thousands separators are refused, since "1,234" would read as 1.234.
ParseRate reads a positive rate with up to six decimals, in millionths.
Signed is Format with a "+" on positive amounts, for balances.
Weighted splits total in proportion to weights, all positive, by largest remainder: each part is rounded down, then the cents left go to the largest remainders, ties to the first. The parts always sum to total.
Settle returns the transfers that bring every balance to zero (balances sum to zero; > 0 means the group owes that member). The largest debt pays the largest credit, ties to the lower index, so each step clears at least one member: at most n-1 transfers, in O(n²) for the few members of a group.