Documentation
¶
Overview ¶
Package money is the ONE exact money value for the Hanzo stack: an amount in a currency, backed by github.com/hanzoai/decimal (big.Int fixed-point). Unlike the int64/uint64 money libraries (which cap around 19 digits — about $9.20 at 18 decimals), this holds a fiat cent AND an 18-decimal on-chain balance with equal exactness, because the value is a big.Int decimal. No float64 ever touches money.
The design is the community-standard money pattern (never floats, integer-exact, Amount + Currency, Split/Allocate so a division loses no sub-unit) with the precision ceiling removed. An Amount is IMMUTABLE. Arithmetic between two Amounts requires the SAME currency, so you can never silently add USD to EUR.
Index ¶
- Variables
- func ParseCents(s string) (int64, error)
- func ParseDecimal(s string) (decimal.Decimal, error)
- func ParseMinor(s string, cur Currency) (int64, error)
- func RateFromFloat(f float64) (decimal.Decimal, error)
- func ScaleMinor(units int64, rate decimal.Decimal) (int64, error)
- func ScaleMinorCeil(units int64, rate decimal.Decimal) (int64, error)
- func ScaleMinorFloor(units int64, rate decimal.Decimal) (int64, error)
- type Amount
- func Avg(amounts ...Amount) (Amount, error)
- func ConvertWithRate(amount Amount, to Currency, rate decimal.Decimal) Amount
- func FromCents(cents int64) Amount
- func FromEUR(minor int64) Amount
- func FromFloat(f float64, cur Currency) Amount
- func FromGBP(minor int64) Amount
- func FromHUSD(wei *big.Int) Amount
- func FromJPY(minor int64) Amount
- func FromMinor(units int64, cur Currency) Amount
- func FromMinorBig(units *big.Int, cur Currency) Amount
- func FromUSD(minor int64) Amount
- func Max(amounts ...Amount) (Amount, error)
- func Min(amounts ...Amount) (Amount, error)
- func New(v decimal.Decimal, cur Currency) Amount
- func Parse(s string, cur Currency) (Amount, error)
- func ParseAmount(s string, defaultCode ...string) (Amount, error)
- func ParseAmountEuropean(s string, defaultCode ...string) (Amount, error)
- func Sum(amounts ...Amount) (Amount, error)
- func Zero(cur Currency) Amount
- func (a Amount) Add(b Amount) (Amount, error)
- func (a Amount) Allocate(ratios ...int) ([]Amount, error)
- func (a Amount) AsMajorUnits() float64
- func (a Amount) Cmp(b Amount) int
- func (a Amount) Currency() Currency
- func (a Amount) Decimal() decimal.Decimal
- func (a Amount) Display() string
- func (a Amount) IsNeg() bool
- func (a Amount) IsZero() bool
- func (a Amount) MajorString() string
- func (a Amount) MarshalJSON() ([]byte, error)
- func (a Amount) Minor() *big.Int
- func (a Amount) MinorString() string
- func (a Amount) Mul(factor decimal.Decimal) Amount
- func (a Amount) Neg() Amount
- func (a *Amount) Scan(src any) error
- func (a Amount) Sign() int
- func (a Amount) Split(n int) ([]Amount, error)
- func (a Amount) String() string
- func (a Amount) Sub(b Amount) (Amount, error)
- func (a *Amount) UnmarshalJSON(b []byte) error
- func (a Amount) Value() (driver.Value, error)
- type Currency
- type Exchange
Constants ¶
This section is empty.
Variables ¶
var ( USD = reg(Currency{"USD", 2, "$", "840", "US Dollar"}) EUR = reg(Currency{"EUR", 2, "€", "978", "Euro"}) GBP = reg(Currency{"GBP", 2, "£", "826", "Pound Sterling"}) JPY = reg(Currency{"JPY", 0, "¥", "392", "Yen"}) CNY = reg(Currency{"CNY", 2, "¥", "156", "Yuan Renminbi"}) CHF = reg(Currency{"CHF", 2, "Fr", "756", "Swiss Franc"}) CAD = reg(Currency{"CAD", 2, "$", "124", "Canadian Dollar"}) AUD = reg(Currency{"AUD", 2, "$", "036", "Australian Dollar"}) NZD = reg(Currency{"NZD", 2, "$", "554", "New Zealand Dollar"}) HKD = reg(Currency{"HKD", 2, "$", "344", "Hong Kong Dollar"}) SGD = reg(Currency{"SGD", 2, "$", "702", "Singapore Dollar"}) INR = reg(Currency{"INR", 2, "₹", "356", "Indian Rupee"}) KRW = reg(Currency{"KRW", 0, "₩", "410", "Won"}) BRL = reg(Currency{"BRL", 2, "R$", "986", "Brazilian Real"}) MXN = reg(Currency{"MXN", 2, "$", "484", "Mexican Peso"}) ZAR = reg(Currency{"ZAR", 2, "R", "710", "Rand"}) SEK = reg(Currency{"SEK", 2, "kr", "752", "Swedish Krona"}) NOK = reg(Currency{"NOK", 2, "kr", "578", "Norwegian Krone"}) DKK = reg(Currency{"DKK", 2, "kr", "208", "Danish Krone"}) PLN = reg(Currency{"PLN", 2, "zł", "985", "Zloty"}) AED = reg(Currency{"AED", 2, "د.إ", "784", "UAE Dirham"}) SAR = reg(Currency{"SAR", 2, "﷼", "682", "Saudi Riyal"}) TRY = reg(Currency{"TRY", 2, "₺", "949", "Turkish Lira"}) RUB = reg(Currency{"RUB", 2, "₽", "643", "Russian Ruble"}) )
Fiat (ISO 4217) — the common set; extend via Register for the long tail.
var ( USDC = reg(Currency{"USDC", 6, "$", "", "USD Coin"}) USDT = reg(Currency{"USDT", 6, "$", "", "Tether"}) BTC = reg(Currency{"BTC", 8, "₿", "", "Bitcoin"}) ETH = reg(Currency{"ETH", 18, "Ξ", "", "Ether"}) HUSD = reg(Currency{"HUSD", 18, "$", "", "Hanzo USD"}) // Hanzo USD stablecoin, ERC-20 )
Crypto / stablecoins — decimals are the token's on-chain decimals so an off-chain Amount and an on-chain balance share one integer scale.
var ( // ErrCurrencyMismatch is returned when an operation combines two different // (non-zero) currencies. ErrCurrencyMismatch = errors.New("money: currencies do not match") // ErrUnknownCurrency is returned when a currency code/number is not in the registry. ErrUnknownCurrency = errors.New("money: unknown currency") // ErrInvalidAmount is returned when an amount string cannot be parsed. ErrInvalidAmount = errors.New("money: invalid amount") // ErrNoRate is returned when an exchange rate is missing. ErrNoRate = errors.New("money: no exchange rate") // ErrNonPositive is returned when a positive value was required (split parts, ratios). ErrNonPositive = errors.New("money: value must be positive") // ErrOverflow is returned when an exact value does not fit the requested // machine type. It is never a rounded or truncated answer: a money conversion // that silently wraps reports a balance nobody holds. ErrOverflow = errors.New("money: value does not fit an int64") )
Sentinel errors, matched with errors.Is.
Functions ¶
func ParseCents ¶ added in v0.2.2
ParseCents is ParseMinor for USD — the inverse of FromCents, and the spelling the dollar-denominated cost APIs want.
func ParseDecimal ¶ added in v0.2.0
ParseDecimal parses just the numeric part (separators handled) — no currency.
func ParseMinor ¶ added in v0.2.2
ParseMinor parses a decimal string of MAJOR units — "19.99", "1,234.56", "-5.00" — into an exact integer count of cur's minor units. It is the inverse of FromMinor, and it is the conversion every ledger edge needs, because upstream billing APIs (DigitalOcean, AWS, GCP) report money as a decimal STRING while every ledger stores an integer.
The whole point is that it goes nowhere near float64. The obvious spelling of this conversion — ParseFloat then multiply by 100 — loses a cent on ordinary prices: 19.99 has no exact binary representation, so f*100 is 1998.999… and truncation gives 1998. Rounding papers over that one, but the float is still an approximation of the money and the error is unbounded in the large: past 2^53 minor units the nearest float64 is not the amount at all. Parsing the digits exactly has no such regime.
Errors are RETURNED, never absorbed. Mapping an unparseable amount to zero is the expensive bug in this family: zero is a legal amount, so a caller that cannot see the difference between "the account spent nothing" and "we could not read what the account spent" reports a confident $0 and the spend silently vanishes from the ledger. The caller decides what an unreadable row means; this function only reports that it was unreadable. An empty string is an error for the same reason — an absent field is a fact the caller must handle, not a zero.
A NEGATIVE PARSES AND STAYS NEGATIVE. On these APIs a negative row is a credit or a refund, and clamping it to zero overstates spend by exactly the amount refunded. Rounding is half-away-from-zero, so a credit rounds like the charge it reverses.
func RateFromFloat ¶ added in v0.2.4
RateFromFloat recovers the exact decimal that a float64 rate was meant to be: the shortest decimal which round-trips to that float, so the 0.1 a human typed comes back as 0.1 and not as 0.1000000000000000055511151231257827.
It exists because schemas predating this package store rates as float64 — a percent column, a commission, a tax rate — and the value has to become exact somewhere before it touches money. That conversion has exactly one correct spelling. Rendering with a fixed precision instead (%.2f and friends) silently truncates any rate finer than the format: a 0.375% fee becomes 0.38%, or with two decimals of a fraction, 0.00.
This does not make the stored float exact — nothing can, the information is already gone. It recovers the decimal the float was the nearest representation OF, which for a human-entered rate is the number that was meant. NaN and ±Inf are not rates and are reported, never mapped to zero: a zero rate is a legal rate, and a caller that cannot tell "no discount" from "unreadable discount" charges full price and calls it a sale.
func ScaleMinor ¶ added in v0.2.4
ScaleMinor scales an integer count of minor units by an exact rate and returns whole minor units — a percentage off a price, a commission on a charge, a tax on a subtotal. It is the third member of the ledger-edge set alongside ParseMinor (string → minor) and MajorString (minor → string): minor × rate → minor, the only one of the three that has to decide what to do with a fraction of a cent.
It decides by ROUNDING HALF AWAY FROM ZERO, the same rule ParseMinor and Rescale use, so one amount cannot depend on which conversion it went through. The obvious spellings instead TRUNCATE — int64(float64(cents)*rate) drops the fraction toward zero, and math.Floor drops it toward negative infinity — and truncation is not a small error but a biased one: 10% off $19.99 is $1.999, which truncates to $1.99 and rounds to $2.00, and the missing cent always falls on the same side of the trade. Across a price book that is a systematic half-cent per line, never random, never self-cancelling.
A rate is dimensionless, so no currency is needed: one minor unit scaled by 0.1 is a tenth of a minor unit whatever the currency's decimals are. Build the rate exactly — decimal.New(10, 2) for 10%, decimal.New(1500, 4) for 1500 basis points, RateFromFloat for a float64 that a legacy schema handed you.
The result is computed in big.Int, so the rate is applied exactly at any magnitude; only the final answer has to fit an int64, and if it does not that is REPORTED. A scaled amount that silently wraps is a balance nobody holds.
func ScaleMinorCeil ¶ added in v0.2.5
ScaleMinorCeil and ScaleMinorFloor are ScaleMinor for the cases where the DIRECTION of the rounding is the policy, not an implementation detail: a fee the house rounds up, a commission the house rounds down and keeps the remainder of. ScaleMinor's half-away-from- zero is the right default for an amount that merely has to be correct; it is the wrong answer for an amount somebody deliberately decided always falls one way.
They round toward +infinity and toward -infinity respectively — math.Ceil and math.Floor, not "away from zero" and "toward zero". On a refund or a credit the sign is negative and the two readings disagree, so the infinities are named rather than the magnitudes.
The point of having them here is that the obvious spellings are WRONG, and wrong in the direction the caller cares most about. math.Ceil(float64(units)*rate) does not round the exact product up, it rounds the FLOAT up, and a float rate is not the rate: 0.07 is held a hair above 0.07, so 700 minor units × 7% lands at 49.000000000000007 and ceilings to 50 — a cent invented on an amount that was exactly 49. math.Floor loses one the same way in reverse, because 0.29 is held a hair below: 100 × 29% lands at 28.999999999999996 and floors to 28. Truncation at least errs consistently; this errs on whichever side the binary representation happened to fall, on amounts that had no fraction to round at all.
Here the product is formed exactly in big.Int and only the true fraction decides, so an exact product is returned unchanged by BOTH functions — which is the property worth having: rounding a whole number is a no-op no matter which way you were going to round.
Types ¶
type Amount ¶
type Amount struct {
// contains filtered or unexported fields
}
Amount is an exact money value: a decimal in a currency. (Currency and the currency registry live in currency.go.)
func Avg ¶ added in v0.2.0
Avg returns the mean, rounded to the currency's decimals (half-away-from-zero). Errors on an empty list or a currency mismatch.
func ConvertWithRate ¶ added in v0.2.0
ConvertWithRate converts amount into `to` at an explicit rate (rounded to `to`'s decimals).
func FromFloat ¶ added in v0.2.0
FromFloat builds an Amount from a float — USE ONLY for converting user input; floats are imprecise, so prefer Parse for exact decimals. The float is rendered at the currency's decimals and parsed exactly from there.
func FromMinor ¶
FromMinor builds an Amount from an integer count of the currency's smallest unit (e.g. cents for USD): value = units × 10^-cur.Decimals.
func FromMinorBig ¶
FromMinorBig is FromMinor for a big.Int count (an on-chain wei balance).
func FromUSD ¶ added in v0.2.0
FromUSD builds a USD amount from integer cents. The FromXXX helpers mirror gocanto's convenience constructors; they take the currency's smallest unit.
func Min ¶ added in v0.2.0
Min returns the smallest amount (same currency). Errors on an empty list or a currency mismatch.
func ParseAmount ¶ added in v0.2.0
ParseAmount parses a human-entered money string — "$1,234.56", "EUR 250,75", "100.50 USD", "£99.99" — into an exact Amount. The currency comes from a leading symbol, or a code token (prefix or suffix), or the optional defaultCode. Thousands separators are handled; a comma-only decimal is treated as a thousands separator (use ParseAmountEuropean for "10,50" == 10.50).
func ParseAmountEuropean ¶ added in v0.2.0
ParseAmountEuropean is ParseAmount but treats a comma-only separator as the DECIMAL point ("€10,50" == €10.50).
func Sum ¶ added in v0.2.0
Sum adds all amounts, which must share a currency (a zero-value operand is agnostic). The empty sum is a zero-value Amount.
func (Amount) Add ¶
Add returns a + b. A zero-value operand adopts the other's currency; two different non-zero currencies error.
func (Amount) Allocate ¶ added in v0.2.0
Allocate distributes the amount across the given integer ratios, summing EXACTLY to the original at the currency's minor-unit granularity — each part floors, then the leftover minor units go one-at-a-time to the leading parts. Ratios must be non-negative with a positive total.
func (Amount) AsMajorUnits ¶ added in v0.2.0
AsMajorUnits returns the value as a float in major units (e.g. dollars) — for DISPLAY only, and for the last inch of an external API whose schema genuinely demands a JSON number; never round-trip money through it, and never store what it returns. When you want the amount as text, that is MajorString, which is exact.
func (Amount) Cmp ¶
Cmp reports −1,0,+1 as a <,==,> b. A zero-value operand is currency-agnostic; two different non-zero currencies panic (comparing USD to EUR is a bug, not a runtime condition — check Currency first for untrusted inputs).
func (Amount) Display ¶
Display renders the value at the currency's decimals with grouped thousands and the currency symbol ("$1,234.56", "¥10000", "100,003.01 HUSD" when no symbol) — the human-facing form of MajorString. Exact, derived from the integer value, not a float.
func (Amount) MajorString ¶ added in v0.2.3
MajorString renders the value in MAJOR units as a plain fixed-scale decimal string — "10.99", "1234567.89", "500" for a zero-decimal currency, "-19.99" for a refund. No symbol, no thousands separators: this is the form every payment API means when it documents an amount field as a decimal string, and it is the exact inverse of ParseMinor.
String() is NOT this conversion, and reaching for it is the easy mistake. String() renders the value's own scale, so it drops trailing zeros: FromCents(100).String() is "1", not "1.00". Both parse back to the same amount — which is why the round-trip law holds for either — but an API handed "1" where its schema says two decimals is a support ticket, and a ledger export that renders $1.00 as "1" no longer reconciles column-wise. MajorString always pads to the currency's decimals.
The other spelling to avoid is the hand-rolled one: print cents/100, a dot, then cents%100. Go's % keeps the sign of the dividend, so -1999 comes out as "-19.-99" and -1 as "0.-1" — and a refund is the most common negative amount there is. Deriving the digits from the rescaled integer has no such case.
func (Amount) MarshalJSON ¶
MarshalJSON emits {"amount":"<decimal>","currency":"USD"} — exact strings, no float.
func (Amount) Minor ¶
Minor returns the value as a big.Int count of the currency's smallest unit (round-half-up on any sub-unit precision) — the storage/on-chain integer.
func (Amount) MinorString ¶
MinorString is the minor-unit integer as a decimal string (the canonical storage form).
func (Amount) Mul ¶
Mul scales the amount by an exact decimal factor (e.g. a token count or a fee rate), keeping the currency.
func (Amount) Split ¶
Split divides the amount into n parts that sum EXACTLY to the original at the currency's minor-unit granularity — the remainder is distributed one minor unit at a time to the leading parts, so no sub-unit is created or lost. n must be positive.
func (*Amount) UnmarshalJSON ¶ added in v0.2.0
UnmarshalJSON parses {"amount","currency"} exactly (a known code resolves to its full Currency; an unknown code is kept as a bare code).
type Currency ¶
type Currency struct {
Code string // ISO-4217 alpha code or custom ("USD", "HUSD")
Decimals int32 // fractional digits of the smallest unit
Symbol string // display grapheme ("$", "€", "₿")
Numeric string // ISO-4217 numeric code ("840"); "" for custom
Name string
}
Currency is a currency's identity: its ISO-4217 (or custom) code, the number of decimal places in its smallest unit (Decimals — USD 2, JPY 0, an 18-decimal ERC-20 18), a display Symbol, and the ISO numeric code where applicable. Custom currencies (crypto, loyalty points) are first-class — construct one directly or Register it.
type Exchange ¶ added in v0.2.0
type Exchange struct {
// contains filtered or unexported fields
}
Exchange is an in-memory table of conversion rates. A rate from → to means one unit of `from` buys `rate` units of `to`. Not safe for concurrent writes; build it once, then read.
func NewExchange ¶ added in v0.2.0
func NewExchange() *Exchange
NewExchange returns an empty rate table.
func (*Exchange) AddRate ¶ added in v0.2.0
AddRate registers (or replaces) the from→to rate from a decimal string ("0.85"). The inverse is NOT implied — add it explicitly if you need both directions.