Documentation
¶
Overview ¶
Package money provides immutable, bounded, exact monetary values backed by math and identified by international currency codes.
Fixed Money never rounds implicitly. Multiplication and division produce RationalMoney, which requires an explicit context and rounding mode before it can return fixed Money. Currency and resolved context mismatches are errors.
Index ¶
- Constants
- Variables
- type AllocationResult
- type Amount
- type Context
- type ContextKind
- type ConversionResult
- type DiscountRate
- type DiscountResult
- type ExchangeRate
- type Money
- func (money Money) Abs() (Money, error)
- func (money Money) Add(other Money) (Money, error)
- func (money Money) Allocate(ctx context.Context, ratios []integer.Integer) (AllocationResult, error)
- func (money Money) Amount() Amount
- func (money Money) Compare(other Money) (int, error)
- func (money Money) Context() Context
- func (money Money) Currency() currency.Code
- func (money Money) Equal(other Money) (bool, error)
- func (money Money) EqualSplit(ctx context.Context, count int) (AllocationResult, error)
- func (money Money) IsZero() bool
- func (money Money) MinorUnits() (integer.Integer, error)
- func (money Money) Mul(ctx context.Context, rate Rate) (RationalMoney, error)
- func (money Money) Neg() (Money, error)
- func (money Money) Quo(ctx context.Context, rate Rate) (RationalMoney, error)
- func (money Money) Ratio(ctx context.Context, other Money) (Ratio, error)
- func (money Money) Sign() int
- func (money Money) String() string
- func (money Money) Sub(other Money) (Money, error)
- func (money Money) Valid() bool
- type MoneyBag
- type Rate
- type Ratio
- type RationalMoney
- type RoundingResult
- type TaxRate
- type TaxResult
Examples ¶
Constants ¶
const MaxAllocationParts = 10_000
MaxAllocationParts bounds output and remainder-distribution work.
const MaxAmountDigits = 256
MaxAmountDigits bounds coefficient input and diagnostic output.
const MaxCashStep uint64 = 1_000_000_000_000_000_000
MaxCashStep bounds cash increments expressed at their context scale.
const MaxMoneyBagEntries = 1_000
MaxMoneyBagEntries bounds heterogeneous output and lookup work.
const MaxRateMagnitude int64 = 1_000_000
MaxRateMagnitude bounds generic multiplication and conversion rates.
const MaxRateSourceBytes = 128
MaxRateSourceBytes bounds persisted and diagnostic source metadata.
const MaxRatioDigits = 64
MaxRatioDigits bounds each positive integer allocation weight.
const MaxScale uint8 = 18
MaxScale bounds decimal work and serialized representation sizes.
const MaxTaxRate int64 = 10
MaxTaxRate bounds a tax multiplier to 1000 percent.
Variables ¶
var ( // ErrUnknownCurrency reports an absent or unrecognized currency identity. ErrUnknownCurrency = errors.New("money: unknown currency") // exponent for a currency, so a default context cannot be inferred. ErrMinorUnitsUnavailable = errors.New("money: currency has no minor-unit metadata") // ErrInvalidContext reports an impossible or unsupported monetary context. ErrInvalidContext = errors.New("money: invalid context") // ErrPrecisionLoss reports input that cannot fit its context without // discarding represented decimal places. ErrPrecisionLoss = errors.New("money: precision loss") // ErrCurrencyMismatch reports arithmetic across different currencies. ErrCurrencyMismatch = errors.New("money: currency mismatch") // ErrContextMismatch reports arithmetic across different precision policies. ErrContextMismatch = errors.New("money: context mismatch") // ErrInvalidMoney reports use of an absent Money zero value. ErrInvalidMoney = errors.New("money: invalid value") // ErrAmountLimit reports an amount outside configured digit or scale bounds. ErrAmountLimit = errors.New("money: amount limit exceeded") // ErrInvalidRate reports a malformed, negative, or excessive exact rate. ErrInvalidRate = errors.New("money: invalid rate") // ErrInvalidAllocation reports an empty, excessive, or nonpositive split. ErrInvalidAllocation = errors.New("money: invalid allocation") // ErrMoneyBagLimit reports excessive distinct currency/context entries. ErrMoneyBagLimit = errors.New("money: bag limit exceeded") )
Functions ¶
This section is empty.
Types ¶
type AllocationResult ¶
type AllocationResult struct {
// contains filtered or unexported fields
}
AllocationResult is an immutable ordered allocation. Earlier parts receive deterministic one-unit remainders.
func (AllocationResult) Parts ¶
func (result AllocationResult) Parts() []Money
Parts returns an independent copy of the ordered allocation.
func (AllocationResult) Sum ¶
func (result AllocationResult) Sum() (Money, error)
Sum returns the conserved total represented by all parts.
type Amount ¶
type Amount struct {
// contains filtered or unexported fields
}
Amount is an immutable bounded monetary decimal. Exact decimal arithmetic is delegated to math; Amount adds only monetary resource policy.
func AmountFromDecimal ¶
AmountFromDecimal applies monetary bounds to an exact math Decimal.
func ParseAmount ¶
ParseAmount parses strict decimal text without a float conversion.
type Context ¶
type Context struct {
// contains filtered or unexported fields
}
Context is an immutable, comparable monetary precision policy. It stores only arithmetic policy; currency identity and metadata remain owned by international.
func AutomaticContext ¶
func AutomaticContext() Context
AutomaticContext constructs a policy that captures exact input scale. The resolved Money context carries that scale, making representation differences explicit during later arithmetic.
func CashContext ¶
CashContext constructs a fixed scale with a positive cash increment. Step is expressed as an integer count of units at scale: scale 2 and step 5 means 0.05.
func CustomContext ¶
CustomContext constructs an application-selected fixed decimal scale.
func DefaultContext ¶
DefaultContext constructs a fixed-scale context from authoritative ISO 4217 metadata. Currencies without an applicable minor-unit exponent require an explicit custom context.
func (Context) CashStep ¶
CashStep returns the integer number of scale-sized units in one cash increment. It is zero for non-cash contexts.
func (Context) Kind ¶
func (context Context) Kind() ContextKind
Kind returns the context's precision policy.
type ContextKind ¶
type ContextKind uint8
ContextKind identifies the monetary precision policy without carrying currency metadata into the arithmetic layer.
const ( // ContextDefault uses the authoritative ISO minor-unit exponent captured // when the context is constructed. ContextDefault ContextKind // ContextCustom uses an application-selected decimal scale. ContextCustom // ContextCash applies a positive cash increment at a selected scale. ContextCash // ContextAutomatic preserves exact input scale until an operation requires // an explicit rounding boundary. ContextAutomatic )
type ConversionResult ¶
type ConversionResult struct {
// contains filtered or unexported fields
}
ConversionResult retains both values, exact rate metadata, and rounding.
func Convert ¶
func Convert(ctx context.Context, source Money, rate ExchangeRate, target Context, mode gomath.RoundingMode) (ConversionResult, error)
Convert applies only the supplied directed exact rate and rounds explicitly into target. It never contacts a live FX service.
Example ¶
package main
import (
"context"
"fmt"
"time"
"github.com/faustbrian/go-international/currency"
gomath "github.com/faustbrian/go-math"
"github.com/faustbrian/go-money"
)
func main() {
euro, _ := currency.Parse("EUR")
dollar, _ := currency.Parse("USD")
euroContext, _ := money.DefaultContext(euro)
dollarContext, _ := money.DefaultContext(dollar)
value, _ := money.Parse("10.00", euro, euroContext)
exact, _ := money.ParseRate("1.1")
rate, _ := money.NewExchangeRate(
euro,
dollar,
exact,
time.Date(2026, 7, 19, 6, 0, 0, 0, time.UTC),
"central-bank-daily",
)
result, _ := money.Convert(
context.Background(),
value,
rate,
dollarContext,
gomath.RoundHalfEven,
)
fmt.Println(result.Converted())
}
Output: 11.00 USD
func (ConversionResult) Converted ¶
func (result ConversionResult) Converted() Money
Converted returns the rounded quote-currency value.
func (ConversionResult) Rate ¶
func (result ConversionResult) Rate() ExchangeRate
Rate returns exact conversion and attribution metadata.
func (ConversionResult) Rounding ¶
func (result ConversionResult) Rounding() RoundingResult
Rounding returns conditions raised at the explicit quote boundary.
func (ConversionResult) Source ¶
func (result ConversionResult) Source() Money
Source returns the original monetary value.
type DiscountRate ¶
type DiscountRate struct {
// contains filtered or unexported fields
}
DiscountRate is an exact validated fraction in the inclusive range [0, 1].
func ParseDiscountRate ¶
func ParseDiscountRate(input string) (DiscountRate, error)
ParseDiscountRate parses an exact discount fraction without floats.
func (DiscountRate) Rate ¶
func (rate DiscountRate) Rate() Rate
Rate returns the generic exact multiplier.
type DiscountResult ¶
type DiscountResult struct {
// contains filtered or unexported fields
}
DiscountResult contains conserved original, discount, and final amounts.
func ApplyDiscount ¶
func ApplyDiscount(ctx context.Context, original Money, rate DiscountRate, mode gomath.RoundingMode) (DiscountResult, error)
ApplyDiscount rounds the discount once and derives final by subtraction so final + discount always equals original.
func (DiscountResult) Discount ¶
func (result DiscountResult) Discount() Money
Discount returns the rounded discount component.
func (DiscountResult) Final ¶
func (result DiscountResult) Final() Money
Final returns original minus discount.
func (DiscountResult) Original ¶
func (result DiscountResult) Original() Money
Original returns the amount before discount.
func (DiscountResult) Rounding ¶
func (result DiscountResult) Rounding() RoundingResult
Rounding returns conditions raised at the monetary boundary.
type ExchangeRate ¶
type ExchangeRate struct {
// contains filtered or unexported fields
}
ExchangeRate is an injected exact directed rate with required attribution. It contains no fetching or ambient live-FX behavior.
func NewExchangeRate ¶
func NewExchangeRate(base, quote currency.Code, rate Rate, observedAt time.Time, source string) (ExchangeRate, error)
NewExchangeRate validates a directed rate and its observation metadata.
func (ExchangeRate) Base ¶
func (rate ExchangeRate) Base() currency.Code
Base returns the source currency.
func (ExchangeRate) Exact ¶
func (rate ExchangeRate) Exact() Rate
Exact returns the exact directed multiplier.
func (ExchangeRate) ObservedAt ¶
func (rate ExchangeRate) ObservedAt() time.Time
ObservedAt returns the rate observation timestamp.
func (ExchangeRate) Quote ¶
func (rate ExchangeRate) Quote() currency.Code
Quote returns the destination currency.
func (ExchangeRate) Source ¶
func (rate ExchangeRate) Source() string
Source returns bounded attribution metadata.
type Money ¶
type Money struct {
// contains filtered or unexported fields
}
Money is an immutable exact decimal amount bound to one currency and one precision context. Its zero value is absent and cannot participate in arithmetic.
func FromMinorUnits ¶
FromMinorUnits constructs Money from an arbitrary-precision integer count of units at the supplied fixed context scale.
func Parse ¶
Parse constructs Money from strict exact decimal text. It never converts through binary floating point. Fixed contexts reject represented fractional places beyond their scale, including trailing zeroes, rather than silently normalizing a context difference.
func (Money) Add ¶
Add returns the exact sum. Currency and resolved context must match.
Example ¶
package main
import (
"fmt"
"github.com/faustbrian/go-international/currency"
"github.com/faustbrian/go-money"
)
func main() {
euro, _ := currency.Parse("EUR")
monetaryContext, _ := money.DefaultContext(euro)
left, _ := money.Parse("12.30", euro, monetaryContext)
right, _ := money.Parse("0.45", euro, monetaryContext)
total, _ := left.Add(right)
fmt.Println(total)
}
Output: 12.75 EUR
func (Money) Allocate ¶
func (money Money) Allocate(ctx context.Context, ratios []integer.Integer) (AllocationResult, error)
Allocate apportions money by positive integer ratios using stable largest remainders. Equal remainders are resolved by original ratio order.
Example ¶
package main
import (
"context"
"fmt"
"github.com/faustbrian/go-international/currency"
"github.com/faustbrian/go-math/integer"
"github.com/faustbrian/go-money"
)
func main() {
euro, _ := currency.Parse("EUR")
monetaryContext, _ := money.DefaultContext(euro)
total, _ := money.Parse("10.00", euro, monetaryContext)
result, _ := total.Allocate(context.Background(), []integer.Integer{
integer.New(1), integer.New(2), integer.New(3),
})
for _, part := range result.Parts() {
fmt.Println(part)
}
}
Output: 1.67 EUR 3.33 EUR 5.00 EUR
func (Money) Equal ¶
Equal reports exact monetary equality after verifying currency and context identity. Mismatched values return an error instead of comparing false.
func (Money) EqualSplit ¶
EqualSplit divides money into count fixed-context parts and distributes any positive or negative minor-unit remainder from the first part onward.
func (Money) MinorUnits ¶
MinorUnits returns the exact arbitrary-precision integer coefficient at the Money context's resolved scale.
func (Money) Mul ¶
Mul returns an exact rational result without applying the Money context's fixed scale.
func (Money) Sign ¶
Sign returns the amount sign. Invalid zero-value Money reports zero; callers can distinguish it with Valid.
func (Money) String ¶
String returns a deterministic diagnostic representation. Locale display belongs to the optional format package.
type MoneyBag ¶
type MoneyBag struct {
// contains filtered or unexported fields
}
MoneyBag is an immutable deterministic collection keyed by exact currency and context identity. Its zero value is an empty bag.
func NewMoneyBag ¶
NewMoneyBag validates and combines values with identical identities.
type Rate ¶
type Rate struct {
// contains filtered or unexported fields
}
Rate is an immutable, exact, nonnegative rational multiplier.
func ParseRateWithMaximum ¶
ParseRateWithMaximum parses a rate while applying an explicit inclusive caller-defined maximum. The maximum accepts the same exact decimal or numerator/denominator syntax as input and must be positive.
type Ratio ¶
type Ratio struct {
// contains filtered or unexported fields
}
Ratio is an immutable exact signed relationship between compatible monetary values.
type RationalMoney ¶
type RationalMoney struct {
// contains filtered or unexported fields
}
RationalMoney is an immutable exact fractional monetary result. It must be rounded explicitly before persistence as fixed-context Money.
func (RationalMoney) Currency ¶
func (money RationalMoney) Currency() currency.Code
Currency returns the monetary identity.
func (RationalMoney) Rational ¶
func (money RationalMoney) Rational() rational.Rational
Rational returns the immutable exact math value.
func (RationalMoney) Round ¶
func (money RationalMoney) Round(target Context, mode gomath.RoundingMode) (Money, RoundingResult, error)
Round creates fixed-context Money at an explicit rounding boundary.
func (RationalMoney) String ¶
func (money RationalMoney) String() string
String returns the normalized exact fraction and currency code.
type RoundingResult ¶
type RoundingResult struct {
// contains filtered or unexported fields
}
RoundingResult describes whether an explicit boundary discarded value.
func (RoundingResult) Conditions ¶
func (result RoundingResult) Conditions() gomath.Condition
Conditions returns the math arithmetic conditions.
func (RoundingResult) Inexact ¶
func (result RoundingResult) Inexact() bool
Inexact reports whether nonzero value was discarded.
type TaxRate ¶
type TaxRate struct {
// contains filtered or unexported fields
}
TaxRate is an exact validated nonnegative tax fraction: 0.24 means 24%.
func ParseTaxRate ¶
ParseTaxRate parses an exact tax fraction without binary floating point.
type TaxResult ¶
type TaxResult struct {
// contains filtered or unexported fields
}
TaxResult contains conserved net, tax, and gross components.
func AddTax ¶
func AddTax(ctx context.Context, net Money, rate TaxRate, mode gomath.RoundingMode) (TaxResult, error)
AddTax calculates tax from a net amount, rounds the tax once, then derives gross by addition so net + tax always equals gross.
func ExtractTax ¶
func ExtractTax(ctx context.Context, gross Money, rate TaxRate, mode gomath.RoundingMode) (TaxResult, error)
ExtractTax derives exact net from gross/(1+rate), rounds net once, then subtracts it from gross so the returned components conserve gross.
func (TaxResult) Rounding ¶
func (result TaxResult) Rounding() RoundingResult
Rounding returns conditions raised at the explicit monetary boundary.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package encoding provides bounded, versioned money wire and persistence adapters.
|
Package encoding provides bounded, versioned money wire and persistence adapters. |
|
Package format renders exact Money values for display.
|
Package format renders exact Money values for display. |
|
Package moneytest provides currency fixtures and conservation assertions for consumers of money.
|
Package moneytest provides currency fixtures and conservation assertions for consumers of money. |