decimal

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package decimal is forge's fixed-point decimal type for money and crypto.

It wraps alpacahq/alpacadecimal (a shopspring/decimal drop-in that keeps the common case in a single int64 and falls back to big.Int only when a value exceeds that envelope) behind a stable forge API, so the backend can change without touching callers. Arithmetic is exact — never float64.

Money adds asset + smallest-unit scale and a sum-conserving Allocate/Split, so distributing a total across parts never creates or loses a minor unit.

Crypto note: the fast int64 path covers ~12 dp and |value| ≲ 9.22M; 8-dp (sat) and 18-dp (wei) values beyond that stay correct via the big.Int fallback. For hot-path crypto balances, prefer integer minor units at the asset's scale (Money carries that scale) and widen to Decimal only for cross-asset or display math.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAssetMismatch = errors.New("decimal: money asset mismatch")
	ErrBadRatios     = errors.New("decimal: ratios must be non-empty and sum positive")
	ErrBadSplit      = errors.New("decimal: split count must be positive")
)
View Source
var Zero = Decimal{}

Zero is the additive identity.

Functions

This section is empty.

Types

type Decimal

type Decimal struct {
	// contains filtered or unexported fields
}

Decimal is an exact fixed-point number.

func FromString

func FromString(s string) (Decimal, error)

FromString parses a decimal string (e.g. "12.34", "-0.000000000000000001").

func New

func New(coefficient int64, exp int32) Decimal

New builds coefficient × 10^exp (e.g. New(1230, -2) == 12.30).

func NewFromBigInt

func NewFromBigInt(value *big.Int, exp int32) Decimal

NewFromBigInt builds value × 10^exp from an arbitrary-precision coefficient.

func NewFromInt

func NewFromInt(i int64) Decimal

NewFromInt builds a whole number.

func RequireFromString

func RequireFromString(s string) Decimal

RequireFromString parses s and panics on error — for constants/tests only.

func (Decimal) Abs

func (d Decimal) Abs() Decimal

func (Decimal) Add

func (d Decimal) Add(o Decimal) Decimal

func (Decimal) BigInt

func (d Decimal) BigInt() *big.Int

func (Decimal) Cmp

func (d Decimal) Cmp(o Decimal) int

func (Decimal) Div

func (d Decimal) Div(o Decimal) Decimal

func (Decimal) DivRound

func (d Decimal) DivRound(o Decimal, precision int32) Decimal

DivRound divides and rounds (half-away-from-zero) to precision places.

func (Decimal) Equal

func (d Decimal) Equal(o Decimal) bool

func (Decimal) Exponent

func (d Decimal) Exponent() int32

func (Decimal) GreaterThan

func (d Decimal) GreaterThan(o Decimal) bool

func (Decimal) GreaterThanOrEqual

func (d Decimal) GreaterThanOrEqual(o Decimal) bool

func (Decimal) InexactFloat64

func (d Decimal) InexactFloat64() float64

InexactFloat64 returns the nearest float64 — lossy; for display/metrics only, never for money arithmetic.

func (Decimal) IntPart

func (d Decimal) IntPart() int64

func (Decimal) IsNegative

func (d Decimal) IsNegative() bool

func (Decimal) IsPositive

func (d Decimal) IsPositive() bool

func (Decimal) IsZero

func (d Decimal) IsZero() bool

func (Decimal) LessThan

func (d Decimal) LessThan(o Decimal) bool

func (Decimal) LessThanOrEqual

func (d Decimal) LessThanOrEqual(o Decimal) bool

func (Decimal) MarshalJSON

func (d Decimal) MarshalJSON() ([]byte, error)

MarshalJSON always emits a quoted string (never a float) so a JSON consumer can't silently lose precision parsing the value as a number.

func (Decimal) Mod

func (d Decimal) Mod(o Decimal) Decimal

func (Decimal) Mul

func (d Decimal) Mul(o Decimal) Decimal

func (Decimal) Neg

func (d Decimal) Neg() Decimal

func (Decimal) Round

func (d Decimal) Round(places int32) Decimal

Round rounds half away from zero to places decimal places.

func (Decimal) RoundBank

func (d Decimal) RoundBank(places int32) Decimal

RoundBank rounds half to even (banker's rounding) — the money default.

func (Decimal) RoundDown

func (d Decimal) RoundDown(places int32) Decimal

RoundDown truncates toward zero to places decimal places.

func (*Decimal) Scan

func (d *Decimal) Scan(v any) error

Scan implements sql.Scanner for NUMERIC/string columns.

func (Decimal) Shift

func (d Decimal) Shift(shift int32) Decimal

Shift moves the decimal point right by shift places (left if negative).

func (Decimal) Sign

func (d Decimal) Sign() int

func (Decimal) String

func (d Decimal) String() string

func (Decimal) StringFixed

func (d Decimal) StringFixed(places int32) string

func (Decimal) Sub

func (d Decimal) Sub(o Decimal) Decimal

func (*Decimal) UnmarshalJSON

func (d *Decimal) UnmarshalJSON(b []byte) error

UnmarshalJSON accepts a quoted string or a bare number; "" / null decode to zero.

func (Decimal) Value

func (d Decimal) Value() (driver.Value, error)

Value implements driver.Valuer — persists as a NUMERIC/string, exact.

type Money

type Money struct {
	// contains filtered or unexported fields
}

Money is an Amount in an asset, rounded to the asset's smallest unit (scale: 2 for USD, 8 for BTC/sat, 18 for ETH/wei).

func NewMoney

func NewMoney(amount Decimal, asset string, scale int32) Money

NewMoney builds a Money, truncating amount to the asset's scale.

func (Money) Add

func (m Money) Add(o Money) (Money, error)

func (Money) Allocate

func (m Money) Allocate(ratios ...int) ([]Money, error)

Allocate splits the amount across the given integer ratios, conserving the sum exactly — no minor unit created or lost. Any indivisible remainder is handed out one unit at a time to the first parts (round-robin).

func (Money) Amount

func (m Money) Amount() Decimal

func (Money) Asset

func (m Money) Asset() string

func (Money) IsZero

func (m Money) IsZero() bool

func (Money) Scale

func (m Money) Scale() int32

func (Money) Split

func (m Money) Split(n int) ([]Money, error)

Split divides the amount into n equal sum-conserving parts.

func (Money) String

func (m Money) String() string

func (Money) Sub

func (m Money) Sub(o Money) (Money, error)

Jump to

Keyboard shortcuts

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