Skip to content Search Apps Documentation Source Content File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

v0 source pure

Package ledger is gnosplit's arithmetic: amounts in cents, how an expense is split, and the fewest transfers that set...

Overview

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.

Constants 2

const MaxCents

1const MaxCents int64 = 10_000_000_000
source

MaxCents bounds one amount: 100 million units of a currency. With rates up to MaxRate the products below stay far inside int64.

const MaxRate

1const MaxRate int64 = 1_000_000_000_000
source

MaxRate bounds a conversion rate, in millionths: 1,000,000 units for one.

Functions 9

func Convert

1func Convert(cents, rate int64) int64
source

Convert turns cents into cents of another currency at rate (millionths), rounding half up. It splits the rate so no product overflows.

func Equal

1func Equal(total int64, n int) []int64
source

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.

func Format

1func Format(cents int64) string
source

Format writes cents as "1,234.56", "-12.00" or "0.00".

func ParseAmount

1func ParseAmount(s string) (cents int64, note string, ok bool)
source

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.

func ParseCents

1func ParseCents(s string) (int64, bool)
source

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.

func ParseRate

1func ParseRate(s string) (int64, bool)
source

ParseRate reads a positive rate with up to six decimals, in millionths.

func Signed

1func Signed(cents int64) string
source

Signed is Format with a "+" on positive amounts, for balances.

func Weighted

1func Weighted(total int64, weights []int64) []int64
source

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.

func Settle

1func Settle(balances []int64) []Transfer
source

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.

Types 1

type Transfer

struct
1type Transfer struct {
2	From, To int
3	Amount   int64
4}
source

Transfer is one payment that settles part of a group: From pays To.

Dependencies

Imports 2

/p/nym-alexiscolin000/gnosplit/ledger/v0

Imported by indexer

Source Files 3