mirror of
https://github.com/hanzoai/money.git
synced 2026-09-30 22:56:51 +00:00
49d60a8d4bd08d01ec3aa13b78eb0c6f00bca531
The v0.2.0 artifact resolved decimal v0.1.1 transitively via consumers' MVS, but the require line still declared v0.1.0. Make the floor honest so any adopter that pins only money gets the exact 18-dp decimal. No API change.
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:
AddSubMulNegCmpSign, same-currency safety (zero-value agnostic) - Allocation:
Split(n)andAllocate(ratios…)— no sub-unit ever lost - Aggregation:
SumMinMaxAvg - Exchange: rate table +
Convert/ConvertWithRate - Parsing:
ParseAmount($1,234.56,EUR 250,75,£99.99), European decimal-comma,ParseDecimal - Formatting:
Display(symbol + thousands separators),AsMajorUnits - Currencies: ISO-4217 set (symbol, numeric code) + crypto (USDC/USDT/BTC/ETH/HUSD) +
Registercustom - 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.
Description
Exact money (Amount+Currency) for Go on big.Int decimal — fiat + 18-dec crypto, no ceiling
87 KiB
Languages
Go
100%