money

package module
v0.2.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 3, 2026 License: BSD-3-Clause Imports: 9 Imported by: 4 modules, 25 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

money

The one exact money type for Go — an Amount in a Currency, backed by hanzoai/decimal (big.Int fixed-point).

Martin Fowler's Money pattern (never floats, integer-exact, Add/Sub/Split/Allocate, same-currency safety) without the precision ceiling. int64/uint64 money libraries cap around 19 digits (~$9.20 at 18 decimals); this holds a fiat cent and an 18-decimal on-chain balance exactly.

price, _ := money.Parse("6.60", money.USD)
cost := price.Mul(decimal.New(200, 0)).Mul(decimal.MustParse("0.000001")) // "0.00132", never 0
bal, _ := money.FromCents(100).Sub(cost)                                  // "0.99868"

money.FromCents(123456).Display()             // "$1,234.56"
parts, _ := money.FromCents(10000).Allocate(50, 30, 20) // $50 / $30 / $20, nothing lost
a, _ := money.ParseAmount("$1,234.56")        // symbol/code/separator aware
money.FromHUSD(wei)                            // 18-dec on-chain stablecoin

Features

  • Exact, uncapped: big.Int core — fiat cents to 18-decimal crypto wei, no float, no ceiling
  • Arithmetic: Add Sub Mul Neg Cmp Sign, same-currency safety (zero-value agnostic)
  • Allocation: Split(n) and Allocate(ratios…) — no sub-unit ever lost
  • Aggregation: Sum Min Max Avg
  • Exchange: rate table + Convert / ConvertWithRate
  • Parsing: ParseAmount ($1,234.56, EUR 250,75, £99.99), European decimal-comma, ParseDecimal
  • Ledger edge: ParseMinor/ParseCents (decimal string → integer minor units), its exact inverse MajorString (integer minor units → fixed-scale decimal string), and ScaleMinor (minor units × rate → minor units, rounded half away from zero) — the set every billing API needs, none of which touches a float. RateFromFloat makes a legacy float64 rate exact first. ScaleMinorCeil/ScaleMinorFloor are the same scaling for amounts whose direction is a policy (a fee rounded up, a commission rounded down) — exact, so math.Ceil cannot invent a cent on a product that was already whole
  • Formatting: MajorString ("10.99", "1.00", "-19.99" — the wire), Display (symbol + thousands separators — the human), AsMajorUnits (float, display only)
  • Currencies: ISO-4217 set (symbol, numeric code) + crypto (USDC/USDT/BTC/ETH/HUSD) + Register custom
  • Integration: json.Marshaler/Unmarshaler, sql.Scanner/driver.Valuer

Packages

Single package github.com/hanzoai/money (Amount, Currency, Exchange, parse/format/aggregate), on github.com/hanzoai/decimal.

Original work (big.Int core written from scratch, not a fork). BSD-3-Clause.

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

Constants

This section is empty.

Variables

View Source
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.

View Source
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.

View Source
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

func ParseCents(s string) (int64, error)

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

func ParseDecimal(s string) (decimal.Decimal, error)

ParseDecimal parses just the numeric part (separators handled) — no currency.

func ParseMinor added in v0.2.2

func ParseMinor(s string, cur Currency) (int64, error)

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

func RateFromFloat(f float64) (decimal.Decimal, error)

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

func ScaleMinor(units int64, rate decimal.Decimal) (int64, error)

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

func ScaleMinorCeil(units int64, rate decimal.Decimal) (int64, error)

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.

func ScaleMinorFloor added in v0.2.5

func ScaleMinorFloor(units int64, rate decimal.Decimal) (int64, error)

ScaleMinorFloor rounds toward -infinity. See ScaleMinorCeil.

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

func Avg(amounts ...Amount) (Amount, error)

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

func ConvertWithRate(amount Amount, to Currency, rate decimal.Decimal) Amount

ConvertWithRate converts amount into `to` at an explicit rate (rounded to `to`'s decimals).

func FromCents

func FromCents(cents int64) Amount

FromCents builds a USD Amount from integer cents.

func FromEUR added in v0.2.0

func FromEUR(minor int64) Amount

FromEUR builds a EUR amount from integer cents.

func FromFloat added in v0.2.0

func FromFloat(f float64, cur Currency) Amount

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 FromGBP added in v0.2.0

func FromGBP(minor int64) Amount

FromGBP builds a GBP amount from integer pence.

func FromHUSD added in v0.2.0

func FromHUSD(wei *big.Int) Amount

FromHUSD builds a Hanzo-USD amount from integer 18-decimal wei.

func FromJPY added in v0.2.0

func FromJPY(minor int64) Amount

FromJPY builds a JPY amount from integer yen (0 decimals).

func FromMinor

func FromMinor(units int64, cur Currency) Amount

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

func FromMinorBig(units *big.Int, cur Currency) Amount

FromMinorBig is FromMinor for a big.Int count (an on-chain wei balance).

func FromUSD added in v0.2.0

func FromUSD(minor int64) Amount

FromUSD builds a USD amount from integer cents. The FromXXX helpers mirror gocanto's convenience constructors; they take the currency's smallest unit.

func Max added in v0.2.0

func Max(amounts ...Amount) (Amount, error)

Max returns the largest amount (same currency).

func Min added in v0.2.0

func Min(amounts ...Amount) (Amount, error)

Min returns the smallest amount (same currency). Errors on an empty list or a currency mismatch.

func New

func New(v decimal.Decimal, cur Currency) Amount

New wraps an exact decimal value in a currency.

func Parse

func Parse(s string, cur Currency) (Amount, error)

Parse reads a decimal string ("6.60", "-0.00132") as an exact Amount in the currency.

func ParseAmount added in v0.2.0

func ParseAmount(s string, defaultCode ...string) (Amount, error)

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

func ParseAmountEuropean(s string, defaultCode ...string) (Amount, error)

ParseAmountEuropean is ParseAmount but treats a comma-only separator as the DECIMAL point ("€10,50" == €10.50).

func Sum added in v0.2.0

func Sum(amounts ...Amount) (Amount, error)

Sum adds all amounts, which must share a currency (a zero-value operand is agnostic). The empty sum is a zero-value Amount.

func Zero

func Zero(cur Currency) Amount

Zero is 0 in the currency.

func (Amount) Add

func (a Amount) Add(b Amount) (Amount, error)

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

func (a Amount) Allocate(ratios ...int) ([]Amount, error)

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

func (a Amount) AsMajorUnits() float64

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

func (a Amount) Cmp(b Amount) int

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) Currency

func (a Amount) Currency() Currency

Currency returns the amount's currency.

func (Amount) Decimal

func (a Amount) Decimal() decimal.Decimal

Decimal returns the exact underlying value.

func (Amount) Display

func (a Amount) Display() string

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) IsNeg

func (a Amount) IsNeg() bool

IsNeg reports whether a < 0.

func (Amount) IsZero

func (a Amount) IsZero() bool

IsZero reports whether a == 0.

func (Amount) MajorString added in v0.2.3

func (a Amount) MajorString() string

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

func (a Amount) MarshalJSON() ([]byte, error)

MarshalJSON emits {"amount":"<decimal>","currency":"USD"} — exact strings, no float.

func (Amount) Minor

func (a Amount) Minor() *big.Int

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

func (a Amount) MinorString() string

MinorString is the minor-unit integer as a decimal string (the canonical storage form).

func (Amount) Mul

func (a Amount) Mul(factor decimal.Decimal) Amount

Mul scales the amount by an exact decimal factor (e.g. a token count or a fee rate), keeping the currency.

func (Amount) Neg

func (a Amount) Neg() Amount

Neg returns −a.

func (*Amount) Scan added in v0.2.0

func (a *Amount) Scan(src any) error

Scan implements sql.Scanner for the "amount|currency" form.

func (Amount) Sign

func (a Amount) Sign() int

Sign reports −1,0,+1 as a <,==,> 0.

func (Amount) Split

func (a Amount) Split(n int) ([]Amount, error)

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) String

func (a Amount) String() string

String renders the exact decimal value ("0.00132").

func (Amount) Sub

func (a Amount) Sub(b Amount) (Amount, error)

Sub returns a − b (same currency rule as Add).

func (*Amount) UnmarshalJSON added in v0.2.0

func (a *Amount) UnmarshalJSON(b []byte) error

UnmarshalJSON parses {"amount","currency"} exactly (a known code resolves to its full Currency; an unknown code is kept as a bare code).

func (Amount) Value added in v0.2.0

func (a Amount) Value() (driver.Value, error)

Value implements driver.Valuer — stored as "amount|currency" ("0.00132|USD"), exact and currency-carrying.

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.

func ByCode added in v0.2.0

func ByCode(code string) (Currency, error)

ByCode looks a currency up by alpha code (case-insensitive).

func ByNumeric added in v0.2.0

func ByNumeric(num string) (Currency, error)

ByNumeric looks a currency up by ISO-4217 numeric code ("840").

func Register added in v0.2.0

func Register(c Currency) Currency

Register adds (or overrides) a currency in the registry and returns it, so custom currencies resolve by code the same as the built-ins.

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

func (e *Exchange) AddRate(from, to Currency, rate string) error

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.

func (*Exchange) Convert added in v0.2.0

func (e *Exchange) Convert(amount Amount, to Currency) (Amount, error)

Convert converts amount into `to`, rounded to `to`'s decimals, using the registered rate.

func (*Exchange) Rate added in v0.2.0

func (e *Exchange) Rate(from, to Currency) (decimal.Decimal, error)

Rate returns the from→to rate (identity 1 when from == to).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址