Documentation
¶
Overview ¶
Package zerodecimal provides a fixed-point decimal type for latency-critical financial code: zero heap allocations on every hot path, panic-free sentinel errors, and arithmetic that is bit-exact against arbitrary-precision references.
Representation ¶
A Decimal is a 24-byte value: value = (neg ? -1 : +1) * coef / 10^prec, where coef is an unsigned 128-bit integer and prec is 0..19. There is no big.Int fallback anywhere: every operation runs on fixed-width integer math, so nothing escapes to the heap. The representable domain is |value| < 2^128 / 10^prec (up to 39 significant digits); operations whose exact result leaves the domain return ErrOverflow instead of degrading.
Error model ¶
Operations that can fail return (Decimal, error) with package-level sentinel errors and never panic. The constructors and the arithmetic operations have panicking twins (MustNew, RequireFromString, MustAdd, ...) for constants, tests, and call sites with proven bounds; the Trunc parsers, NewFromHiLo, IntPart, the Unmarshal methods, and Scan have none. Operations that cannot fail (Neg, Abs, rounding, comparisons, formatting) return Decimal directly and chain freely.
Allocations ¶
Parsing, arithmetic, comparison, rounding, conversions, the Append* methods, and every Unmarshal and Scan path allocate nothing — on success and error paths alike. String and StringFixed cost exactly one allocation (the returned string; values within ±1000.00 at up to two decimal places are served from a precomputed cache for zero), the Marshal* methods exactly one (the returned slice), and SQL Value at most two. The default test suite enforces these counts exactly with testing.AllocsPerRun.
Equality ¶
Arithmetic does not trim trailing fractional zeros, so == on Decimal values is representation equality, not numeric equality: a result equal to 1.50 differs from a parsed 1.5 under ==. Use Equal or Cmp for numeric comparison. Parsing and string constructors trim trailing fractional zeros; NewFromHiLo keeps the supplied precision verbatim for raw interop. Zero is always the zero value Decimal{} — no operation produces a negative zero.
Build tags ¶
DefaultPrec — the precision Div targets and strict parsing accepts — is a compile-time constant: 19 by default, lowered to 9 or 12 with the zerodecimal_prec9 or zerodecimal_prec12 build tags. The full test suites assume DefaultPrec = 19; the zerodecimal_prec9 and zerodecimal_prec12 configurations are verified at compile + vet level only in v1. The zerodecimal_nostrcache tag compiles out the small-value string cache that String and Value consult.
Example ¶
package main
import (
"fmt"
zerodecimal "github.com/AlexandrosKyriakakis/zerodecimal"
)
func main() {
price, err := zerodecimal.NewFromString("99.99")
if err != nil {
panic(err)
}
qty := zerodecimal.NewFromInt(3)
total, err := price.Mul(qty)
if err != nil {
panic(err)
}
fmt.Println(total)
fmt.Println(total.StringFixed(4))
}
Output: 299.97 299.9700
Index ¶
- Constants
- Variables
- type Decimal
- func Avg(first Decimal, rest ...Decimal) (Decimal, error)
- func Max(first Decimal, rest ...Decimal) Decimal
- func Min(first Decimal, rest ...Decimal) Decimal
- func MustAvg(first Decimal, rest ...Decimal) Decimal
- func MustNew(value int64, exp int32) Decimal
- func MustSum(first Decimal, rest ...Decimal) Decimal
- func New(value int64, exp int32) (Decimal, error)
- func NewFromFloat(f float64) (Decimal, error)
- func NewFromFloat32(f float32) (Decimal, error)
- func NewFromHiLo(neg bool, hi, lo uint64, prec uint8) (Decimal, error)
- func NewFromInt(v int64) Decimal
- func NewFromInt32(v int32) Decimal
- func NewFromString(s string) (Decimal, error)
- func NewFromStringTrunc(s string) (Decimal, error)
- func NewFromUint64(v uint64) Decimal
- func ParseBytes(b []byte) (Decimal, error)
- func ParseBytesTrunc(b []byte) (Decimal, error)
- func RequireFromFloat(f float64) Decimal
- func RequireFromString(s string) Decimal
- func Sum(first Decimal, rest ...Decimal) (Decimal, error)
- func (d Decimal) Abs() Decimal
- func (d Decimal) Add(e Decimal) (Decimal, error)
- func (d Decimal) AppendBinary(b []byte) ([]byte, error)
- func (d Decimal) AppendFixed(b []byte, places uint8) []byte
- func (d Decimal) AppendText(b []byte) ([]byte, error)
- func (d Decimal) Ceil() Decimal
- func (d Decimal) Cmp(e Decimal) int
- func (d Decimal) Div(e Decimal) (Decimal, error)
- func (d Decimal) Equal(e Decimal) bool
- func (d Decimal) Floor() Decimal
- func (d Decimal) GreaterThan(e Decimal) bool
- func (d Decimal) GreaterThanOrEqual(e Decimal) bool
- func (d Decimal) InexactFloat64() float64
- func (d Decimal) IntPart() (int64, error)
- func (d Decimal) IsNegative() bool
- func (d Decimal) IsPositive() bool
- func (d Decimal) IsZero() bool
- func (d Decimal) LessThan(e Decimal) bool
- func (d Decimal) LessThanOrEqual(e Decimal) bool
- func (d Decimal) MarshalBinary() ([]byte, error)
- func (d Decimal) MarshalJSON() ([]byte, error)
- func (d Decimal) MarshalText() ([]byte, error)
- func (d Decimal) Mod(e Decimal) (Decimal, error)
- func (d Decimal) Mul(e Decimal) (Decimal, error)
- func (d Decimal) MustAdd(e Decimal) Decimal
- func (d Decimal) MustDiv(e Decimal) Decimal
- func (d Decimal) MustMod(e Decimal) Decimal
- func (d Decimal) MustMul(e Decimal) Decimal
- func (d Decimal) MustQuoRem(e Decimal) (Decimal, Decimal)
- func (d Decimal) MustSub(e Decimal) Decimal
- func (d Decimal) Neg() Decimal
- func (d Decimal) Prec() uint8
- func (d Decimal) QuoRem(e Decimal) (Decimal, Decimal, error)
- func (d Decimal) Round(places uint8) Decimal
- func (d Decimal) RoundBank(places uint8) Decimal
- func (d Decimal) RoundCeil(places uint8) Decimal
- func (d Decimal) RoundDown(places uint8) Decimal
- func (d Decimal) RoundFloor(places uint8) Decimal
- func (d Decimal) RoundUp(places uint8) Decimal
- func (d *Decimal) Scan(src any) error
- func (d Decimal) Sign() int
- func (d Decimal) String() string
- func (d Decimal) StringFixed(places uint8) string
- func (d Decimal) Sub(e Decimal) (Decimal, error)
- func (d Decimal) ToHiLo() (neg bool, hi, lo uint64, prec uint8)
- func (d Decimal) Truncate(places uint8) Decimal
- func (d *Decimal) UnmarshalBinary(data []byte) error
- func (d *Decimal) UnmarshalJSON(data []byte) error
- func (d *Decimal) UnmarshalText(b []byte) error
- func (d Decimal) Value() (driver.Value, error)
- type NullDecimal
- func (n NullDecimal) MarshalJSON() ([]byte, error)
- func (n NullDecimal) MarshalText() ([]byte, error)
- func (n *NullDecimal) Scan(src any) error
- func (n *NullDecimal) UnmarshalJSON(data []byte) error
- func (n *NullDecimal) UnmarshalText(b []byte) error
- func (n NullDecimal) Value() (driver.Value, error)
Examples ¶
Constants ¶
const DefaultPrec uint8 = 19
DefaultPrec is the target number of fractional digits for division results and the maximum accepted by strict parsing. It is a compile-time constant (never a runtime knob) so precision checks fold to immediate compares; build with -tags zerodecimal_prec9 or zerodecimal_prec12 to lower it.
const MaxPrec uint8 = 19
MaxPrec is the maximum number of fractional digits a Decimal can carry. It is bounded by the 10^19 < 2^64 invariant the scaling tables rely on.
Variables ¶
var ( // ErrOverflow is returned when the exact result of an operation does not // fit a 128-bit coefficient at the result precision. ErrOverflow = errors.New("zerodecimal: value overflows 128-bit coefficient") // ErrDivideByZero is returned by Div, QuoRem, and Mod when the divisor is zero. ErrDivideByZero = errors.New("zerodecimal: division by zero") // ErrPrecOutOfRange is returned when a requested or parsed precision // exceeds MaxPrec fractional digits. ErrPrecOutOfRange = errors.New("zerodecimal: precision out of range") // ErrInvalidFormat is returned when parsing input that is not a valid decimal. ErrInvalidFormat = errors.New("zerodecimal: invalid format") // ErrEmptyString is returned when parsing empty input. ErrEmptyString = errors.New("zerodecimal: empty input") // ErrMaxStrLen is returned when parsing input longer than 200 bytes. ErrMaxStrLen = errors.New("zerodecimal: input exceeds 200 bytes") // ErrInvalidFloat is returned by float constructors for NaN or infinities. ErrInvalidFloat = errors.New("zerodecimal: NaN or Infinity") // ErrIntPartOverflow is returned when the integer part does not fit int64. ErrIntPartOverflow = errors.New("zerodecimal: integer part overflows int64") // ErrInvalidBinaryData is returned by UnmarshalBinary for malformed input. ErrInvalidBinaryData = errors.New("zerodecimal: invalid binary data") // ErrScanNil is returned when scanning SQL NULL into a Decimal; use // NullDecimal for nullable columns. ErrScanNil = errors.New("zerodecimal: cannot scan nil into Decimal") // ErrScanType is returned when scanning an unsupported source type. ErrScanType = errors.New("zerodecimal: unsupported Scan source type") )
Sentinel errors for every fallible operation in the package. They are returned bare (never wrapped) on hot paths so that error returns stay allocation-free; match with errors.Is.
var One = NewFromInt(1)
One is the decimal one with precision 0.
var Zero = Decimal{}
Zero is the canonical decimal zero, identical to the zero value Decimal{}.
Functions ¶
This section is empty.
Types ¶
type Decimal ¶
type Decimal struct {
// contains filtered or unexported fields
}
Decimal is a fixed-point decimal number:
value = (neg ? -1 : +1) · coef / 10^prec
with coef an unsigned 128-bit coefficient and prec 0..MaxPrec fractional digits. A Decimal is a 24-byte pointer-free value — copy it freely; the zero value is the canonical decimal zero and ready to use. The field order is load-bearing: coef first keeps the hot-path limb loads at offset zero.
INVARIANT (canonical zero): a Decimal with a zero coefficient is always exactly Decimal{} — neg false, prec 0. Every code path that can produce zero routes through newDecimal, so a negative zero cannot exist and sign predicates need no zero checks.
Arithmetic never trims trailing fractional zeros, so == compares representations, not numbers (1.5 != 1.50 under ==); use Equal or Cmp for numeric comparison.
func Avg ¶
Avg returns the arithmetic mean (first + rest...)/(1 + len(rest)) with Div's adaptive precision and error contract; the intermediate Sum can also overflow.
func Max ¶
Max returns the numerically largest argument (1.5 and 1.50 compare equal; the first of equal values wins). It is infallible — comparison never overflows.
func Min ¶
Min returns the numerically smallest argument (1.5 and 1.50 compare equal; the first of equal values wins). It is infallible — comparison never overflows.
func MustNew ¶
MustNew is New for arguments with proven bounds: it panics instead of returning an error.
func New ¶
New returns value · 10^exp. A positive exp scales the coefficient and returns ErrOverflow when the result exceeds 128 bits (always, once exp > 38 with a nonzero value). A negative exp becomes fractional precision; when it exceeds MaxPrec, factors of ten are stripped from value while it stays exact, and ErrPrecOutOfRange is returned if the precision still cannot fit. New(0, exp) is Zero for every exp.
func NewFromFloat ¶
NewFromFloat converts f through its shortest decimal representation — the digits strconv prints for the exact bits of f — with no silent rounding: NaN and infinities return ErrInvalidFloat, |f| ≥ 2^128 returns ErrOverflow, and a nonzero |f| below 10^-19 or a shortest form needing more than MaxPrec fractional digits returns ErrPrecOutOfRange. For lossy ingestion of arbitrary floats, make the rounding explicit instead: NewFromStringTrunc(strconv.FormatFloat(f, 'f', -1, 64)).
func NewFromFloat32 ¶
NewFromFloat32 is NewFromFloat with the shortest form computed at 32-bit precision: NewFromFloat32(0.1) is exactly 0.1, not the longer expansion of float64(float32(0.1)).
func NewFromHiLo ¶
NewFromHiLo assembles a Decimal from its raw representation — sign, 128-bit coefficient limbs, and fractional precision — the inverse of ToHiLo, for zero-cost interop with other 128-bit decimal layouts. The coefficient is taken verbatim: trailing fractional zeros are NOT trimmed, though a zero coefficient still collapses to the canonical Decimal{}. Returns ErrPrecOutOfRange when prec > MaxPrec.
func NewFromInt ¶
NewFromInt returns the Decimal with the exact value of v and precision 0.
func NewFromInt32 ¶
NewFromInt32 returns the Decimal with the exact value of v and precision 0.
func NewFromString ¶
NewFromString parses a decimal literal — optional sign, digits, optional fraction, optional e/E exponent — into the exact Decimal it denotes. Values needing more than MaxPrec fractional digits return ErrPrecOutOfRange (use NewFromStringTrunc to truncate instead), coefficients past 2^128-1 return ErrOverflow, and grammar violations return ErrInvalidFormat. Stricter than shopspring/decimal: "1." and ".1" are rejected — both sides of the dot need a digit. The result is canonical (trailing fractional zeros trimmed) and parsing never allocates.
Example ¶
package main
import (
"errors"
"fmt"
zerodecimal "github.com/AlexandrosKyriakakis/zerodecimal"
)
func main() {
d, err := zerodecimal.NewFromString("1.5000")
if err != nil {
panic(err)
}
fmt.Println(d) // trailing fractional zeros trim at parse time
sci, err := zerodecimal.NewFromString("1.23e4")
if err != nil {
panic(err)
}
fmt.Println(sci)
_, err = zerodecimal.NewFromString(".5") // both sides of the dot need a digit
fmt.Println(errors.Is(err, zerodecimal.ErrInvalidFormat))
}
Output: 1.5 12300 true
func NewFromStringTrunc ¶
NewFromStringTrunc is NewFromString except that a value needing more than MaxPrec fractional digits is truncated toward zero at prec MaxPrec — possibly to exactly zero — instead of returning ErrPrecOutOfRange. The truncation also covers mantissas longer than the 128-bit coefficient can hold: input with forty or more significant digits parses whenever the truncated value itself is representable, and ErrOverflow remains only for values whose truncated coefficient exceeds 2^128-1. All other errors are unchanged.
Example ¶
package main
import (
"errors"
"fmt"
zerodecimal "github.com/AlexandrosKyriakakis/zerodecimal"
)
func main() {
// Strict parsing rejects values needing more than MaxPrec fractional digits...
_, err := zerodecimal.NewFromString("0.123456789012345678901")
fmt.Println(errors.Is(err, zerodecimal.ErrPrecOutOfRange))
// ...while the Trunc variant truncates toward zero at MaxPrec instead.
d, err := zerodecimal.NewFromStringTrunc("0.123456789012345678901")
if err != nil {
panic(err)
}
fmt.Println(d)
}
Output: true 0.1234567890123456789
func NewFromUint64 ¶
NewFromUint64 returns the Decimal with the exact value of v and precision 0.
func ParseBytes ¶
ParseBytes is NewFromString over a byte slice, avoiding any string conversion: b is read in place, never retained, and never modified.
func ParseBytesTrunc ¶
ParseBytesTrunc is NewFromStringTrunc over a byte slice (see ParseBytes).
func RequireFromFloat ¶
RequireFromFloat is NewFromFloat for values with proven bounds: it panics instead of returning an error.
func RequireFromString ¶
RequireFromString is NewFromString for literals with proven validity, such as constants and test fixtures: it panics instead of returning an error.
func Sum ¶
Sum returns first + rest[0] + ... + rest[n-1], folding Add left to right, and stops with ErrOverflow as soon as a partial sum overflows.
func (Decimal) Add ¶
Add returns d + e computed exactly at precision max(d.prec, e.prec). ErrOverflow is returned iff the exact coefficient at that precision does not fit 128 bits — alignment itself can never fail, and opposite-sign operands can never overflow. Both same-precision cases run inline: same signs add as a single 128-bit magnitude add, opposite signs subtract as a single 128-bit magnitude subtract with a conditional two's-complement fix keyed on the borrow. Only differing precisions outline, straight into addUnaligned.
Example ¶
package main
import (
"fmt"
zerodecimal "github.com/AlexandrosKyriakakis/zerodecimal"
)
func main() {
a := zerodecimal.RequireFromString("0.1")
b := zerodecimal.RequireFromString("0.2")
fmt.Println(a.MustAdd(b))
x, y := 0.1, 0.2 // the float64 arithmetic that decimals exist to avoid
fmt.Println(x + y)
}
Output: 0.3 0.30000000000000004
func (Decimal) AppendBinary ¶
AppendBinary implements encoding.BinaryAppender, appending the MarshalBinary encoding of d to b and returning the extended slice. The error is always nil. It allocates only if b lacks capacity.
func (Decimal) AppendFixed ¶
AppendFixed appends the StringFixed rendering of d to b and returns the extended slice. It allocates only if b lacks capacity.
func (Decimal) AppendText ¶
AppendText appends the canonical decimal representation of d to b and returns the extended slice, matching the encoding.TextAppender shape. The error is always nil. Values inside the small-value cache window append the precomputed string (byte-identical to appendCanonical's output, since the cache is built through it — see cache.go); everything else renders inline. It allocates only if b lacks capacity.
func (Decimal) Ceil ¶
Ceil returns the smallest integer value ≥ d: 2.5 → 3 and -2.5 → -2, with precision 0 unless d already had none.
func (Decimal) Cmp ¶
Cmp returns -1 when d < e, 0 when d == e numerically, and +1 when d > e. Decimals of different precision compare by value: 1.5 equals 1.50. Comparison is infallible — no precision alignment can overflow.
func (Decimal) Div ¶
Div returns d ÷ e truncated toward zero at adaptive precision: the result is trunc(d/e · 10^p) with precision p, for the LARGEST p ≤ DefaultPrec whose quotient coefficient fits 128 bits. Exact results therefore keep p = DefaultPrec (their trailing zeros are not trimmed), while quotients of huge magnitudes degrade precision gracefully instead of failing. ErrOverflow only when even the integer quotient (p = 0) does not fit; ErrDivideByZero when e is zero. Dividing zero by anything nonzero is Zero.
The common case is fast-pathed: p = DefaultPrec is the maximum precision, so when its quotient already fits 128 bits (the overwhelmingly common shape) it trivially realizes the largest-p contract and Div returns immediately, skipping the bound estimate, descent loop and p+1 probe entirely. Those run only on the degradation path, when even p = DefaultPrec overflows.
Example ¶
package main
import (
"errors"
"fmt"
zerodecimal "github.com/AlexandrosKyriakakis/zerodecimal"
)
func main() {
third, err := zerodecimal.NewFromInt(1).Div(zerodecimal.NewFromInt(3))
if err != nil {
panic(err)
}
fmt.Println(third) // truncated at DefaultPrec fractional digits
fmt.Println(zerodecimal.NewFromInt(1).MustDiv(zerodecimal.NewFromInt(8)))
_, err = zerodecimal.NewFromInt(1).Div(zerodecimal.Zero)
fmt.Println(errors.Is(err, zerodecimal.ErrDivideByZero))
}
Output: 0.3333333333333333333 0.125 true
func (Decimal) Equal ¶
Equal reports whether d and e are numerically equal, ignoring trailing fractional zeros (1.5 equals 1.50). Use it instead of ==, which is representation equality. Identical representations short-circuit before any comparison work.
Example ¶
package main
import (
"fmt"
zerodecimal "github.com/AlexandrosKyriakakis/zerodecimal"
)
func main() {
a := zerodecimal.RequireFromString("1.5")
// An arithmetic result keeps its precision: 0.75 + 0.75 is 1.50, not 1.5.
b := zerodecimal.RequireFromString("0.75").MustAdd(zerodecimal.RequireFromString("0.75"))
fmt.Println(a == b) // == compares representations
fmt.Println(a.Equal(b)) // Equal compares values
fmt.Println(a.Cmp(b))
}
Output: false true 0
func (Decimal) Floor ¶
Floor returns the largest integer value ≤ d: 2.5 → 2 and -2.5 → -3, with precision 0 unless d already had none.
func (Decimal) GreaterThan ¶
GreaterThan reports whether d > e.
func (Decimal) GreaterThanOrEqual ¶
GreaterThanOrEqual reports whether d ≥ e.
func (Decimal) InexactFloat64 ¶
InexactFloat64 returns the float64 nearest to d. The conversion goes through strconv.ParseFloat over the canonical string for correct round-to-nearest-even; every Decimal is finite and |d| < 2^128 stays well inside float64 range, so the parse cannot fail.
func (Decimal) IntPart ¶
IntPart returns the integer part of d truncated toward zero, or ErrIntPartOverflow when it does not fit int64. MinInt64 round-trips: NewFromInt(math.MinInt64).IntPart() returns math.MinInt64.
func (Decimal) IsNegative ¶
IsNegative reports whether d < 0. The bare flag is exact because the canonical-zero invariant keeps zero unsigned.
func (Decimal) LessThanOrEqual ¶
LessThanOrEqual reports whether d ≤ e.
func (Decimal) MarshalBinary ¶
MarshalBinary implements encoding.BinaryMarshaler using the compact wire format documented at the binFlag constants: 10 bytes when the coefficient fits one limb, 18 otherwise, NOT udecimal-compatible. It costs exactly one allocation — the result, sized exactly.
func (Decimal) MarshalJSON ¶
MarshalJSON implements json.Marshaler, rendering d as a double-quoted canonical decimal string ("1.23") — ALWAYS quoted, because a bare JSON number is read as float64 by most consumers and would silently lose digits past 2^53. The rendering happens in a stack buffer, so the one allocation is the exactly-sized result; it deliberately never aliases the small-value string cache, since callers own the returned bytes and may mutate them.
Example ¶
package main
import (
"encoding/json"
"fmt"
zerodecimal "github.com/AlexandrosKyriakakis/zerodecimal"
)
func main() {
type Order struct {
Price zerodecimal.Decimal `json:"price"`
Qty zerodecimal.Decimal `json:"qty"`
}
data, err := json.Marshal(Order{
Price: zerodecimal.RequireFromString("19.99"),
Qty: zerodecimal.NewFromInt(2),
})
if err != nil {
panic(err)
}
fmt.Println(string(data)) // always quoted: bare JSON numbers lose digits past 2^53
}
Output: {"price":"19.99","qty":"2"}
func (Decimal) MarshalText ¶
MarshalText implements encoding.TextMarshaler, returning the canonical decimal representation of d (the exact bytes String produces) in a freshly allocated slice. It costs exactly one allocation — the result, sized exactly (the rendering happens in a stack buffer first).
func (Decimal) Mod ¶
Mod returns the remainder of QuoRem: d - trunc(d/e)·e, carrying the sign of d and precision max(d.prec, e.prec), with the same error contract.
func (Decimal) Mul ¶
Mul returns d × e. The exact product carries d.prec + e.prec fractional digits (at most 2·MaxPrec); when that exceeds DefaultPrec the excess digits are truncated toward zero, so the result precision is min(d.prec + e.prec, DefaultPrec). ErrOverflow iff the truncated coefficient does not fit 128 bits. One-limb products that need no rescale take a single hardware multiply; everything else outlines into mulSlow.
func (Decimal) MustQuoRem ¶
MustQuoRem is QuoRem for operands with proven bounds: it panics on error.
func (Decimal) QuoRem ¶
QuoRem returns the truncated quotient q = trunc(d/e) and the remainder r = d - q·e (T-division, matching Go's integer operators and the shopspring/udecimal convention). q has precision 0 and the sign of d.neg != e.neg; r has precision f = max(d.prec, e.prec), the sign of d, and |r| < |e|; the identity d = q·e + r always holds. ErrDivideByZero when e is zero; ErrOverflow when the quotient does not fit a 128-bit coefficient, or when the divisor aligned to precision f does not.
Example ¶
package main
import (
"fmt"
zerodecimal "github.com/AlexandrosKyriakakis/zerodecimal"
)
func main() {
q, r := zerodecimal.RequireFromString("7.5").MustQuoRem(zerodecimal.NewFromInt(2))
fmt.Println(q, r)
}
Output: 3 1.5
func (Decimal) Round ¶
Round rounds d to places fractional digits with ties away from zero (shopspring Round semantics): 2.5 → 3 and -2.5 → -3. places counts fractional digits only; negative places (rounding integer positions) are unsupported by design, which keeps the whole rounding family infallible. places ≥ d.Prec() returns d unchanged.
Example ¶
package main
import (
"fmt"
zerodecimal "github.com/AlexandrosKyriakakis/zerodecimal"
)
func main() {
fmt.Println(zerodecimal.RequireFromString("2.5").Round(0)) // ties away from zero
fmt.Println(zerodecimal.RequireFromString("-2.5").Round(0))
fmt.Println(zerodecimal.RequireFromString("1.2345").Round(2))
}
Output: 3 -3 1.23
func (Decimal) RoundBank ¶
RoundBank rounds d to places fractional digits with ties to even (banker's rounding): 2.5 → 2, 3.5 → 4, -2.5 → -2. places ≥ d.Prec() returns d unchanged.
Example ¶
package main
import (
"fmt"
zerodecimal "github.com/AlexandrosKyriakakis/zerodecimal"
)
func main() {
fmt.Println(zerodecimal.RequireFromString("2.5").RoundBank(0)) // ties to even
fmt.Println(zerodecimal.RequireFromString("3.5").RoundBank(0))
fmt.Println(zerodecimal.RequireFromString("-2.5").RoundBank(0))
}
Output: 2 4 -2
func (Decimal) RoundCeil ¶
RoundCeil rounds d to places fractional digits toward +∞: 1.01 → 1.1 but -1.09 → -1.0 at one place. places ≥ d.Prec() returns d unchanged.
func (Decimal) RoundDown ¶
RoundDown rounds d to places fractional digits toward zero, simply dropping the excess digits: 1.09 → 1.0 and -1.09 → -1.0 at one place. places ≥ d.Prec() returns d unchanged.
func (Decimal) RoundFloor ¶
RoundFloor rounds d to places fractional digits toward -∞: -1.01 → -1.1 but 1.09 → 1.0 at one place. places ≥ d.Prec() returns d unchanged.
func (Decimal) RoundUp ¶
RoundUp rounds d to places fractional digits away from zero: any dropped nonzero remainder steps the magnitude up, so 1.01 → 1.1 and -1.01 → -1.1 at one place. places ≥ d.Prec() returns d unchanged.
func (*Decimal) Scan ¶
Scan implements sql.Scanner, populating d from a value produced by a database driver. string and []byte parse as strict decimal literals (NewFromString grammar, scientific notation included); int64, int32, int, and uint64 convert exactly at precision 0; float64 converts through its shortest decimal representation (NewFromFloat semantics — NaN and infinities return ErrInvalidFloat). A nil src (SQL NULL) returns ErrScanNil; use NullDecimal for nullable columns. Any other type returns an error wrapping ErrScanType. d is left unchanged on every error path.
Every supported conversion is allocation-free. The unsupported-type branch is the package's one fmt call: it allocates the wrapped error, which is acceptable on a path that is cold by construction — a column's driver type does not change between rows.
func (Decimal) String ¶
String returns the canonical decimal representation of d (the exact form appendCanonical produces). Values inside the small-value cache window return the precomputed string with zero allocations; everything else costs exactly one string allocation.
func (Decimal) StringFixed ¶
StringFixed returns d rounded to places fractional digits (ties away from zero, like Round) and formatted with EXACTLY places fractional digits — zero-padded, never trimmed — so StringFixed(3) of 1.5 is "1.500". With places 0 the result has no decimal point. It costs one string allocation.
func (Decimal) Sub ¶
Sub returns d - e computed exactly at precision max(d.prec, e.prec), with the same overflow contract as Add. Both same-precision cases run inline: opposite signs subtract as a magnitude add, same signs as a single 128-bit magnitude subtract with a conditional two's-complement fix keyed on the borrow. Only differing precisions outline, straight into addUnaligned (the shared differing-precision arm) with the sign of e flipped — a zero e keeps its canonical unsigned form, so its sign is never flipped.
func (Decimal) ToHiLo ¶
ToHiLo exposes the raw representation of d, such that d = (neg ? -1 : +1) · (hi·2^64 + lo) / 10^prec. It is total: every Decimal round-trips exactly through NewFromHiLo.
func (Decimal) Truncate ¶
Truncate drops every fractional digit past places, identical to RoundDown. places ≥ d.Prec() returns d unchanged.
func (*Decimal) UnmarshalBinary ¶
UnmarshalBinary implements encoding.BinaryUnmarshaler for the MarshalBinary wire format. Validation is strict — any violation returns ErrInvalidBinaryData and leaves d unchanged:
- the length must be exactly 10 or 18 bytes, consistent with the high-limb flag bit (every truncation is caught, never read past)
- reserved flag bits 2..7 must be zero (the format-version guard)
- prec must not exceed MaxPrec
- a present high limb must be nonzero (the canonical encoding emits the short form whenever it can, so a zero high limb marks a foreign or corrupted encoder)
A zero coefficient is normalized through newDecimal regardless of the encoded sign and precision, so a foreign encoder's "-0.000" still decodes to the canonical Decimal{}. It never allocates.
func (*Decimal) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler, accepting both the package's own quoted form ("1.23") and bare JSON numbers — the strict parse grammar includes scientific notation, so float-encoded numbers like 1.5e-7 decode exactly. A literal null is a no-op (the stdlib convention, mirroring json.Unmarshal on pointers); use NullDecimal to distinguish null from a value. Exactly one balanced quote pair is stripped, so "null" in quotes and unbalanced quotes are parse errors. Errors are the bare parse sentinels and leave d unchanged. It never allocates.
Example ¶
package main
import (
"encoding/json"
"fmt"
zerodecimal "github.com/AlexandrosKyriakakis/zerodecimal"
)
func main() {
var d zerodecimal.Decimal
// Bare JSON numbers decode exactly too, scientific notation included.
if err := json.Unmarshal([]byte(`1.5e-7`), &d); err != nil {
panic(err)
}
fmt.Println(d)
}
Output: 0.00000015
func (*Decimal) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler, parsing the strict decimal literal grammar of NewFromString (scientific notation included). Errors are the bare parse sentinels — match with errors.Is — and d is left unchanged on every error path. It never allocates.
func (Decimal) Value ¶
Value implements driver.Valuer, rendering d as its canonical string — the one decimal wire form every SQL driver and database agree on, with no float rounding and no driver-side numeric truncation. Values inside the small-value cache window return a pre-boxed driver.Value with zero allocations; everything else costs exactly two — the canonical string plus boxing its header into the interface (the bytes themselves are shared, not copied).
type NullDecimal ¶
NullDecimal is a Decimal that can represent SQL NULL: Valid false means NULL, and then Decimal holds the zero value. It follows the database/sql Null* convention so nullable columns scan without errors.
Example ¶
package main
import (
"fmt"
zerodecimal "github.com/AlexandrosKyriakakis/zerodecimal"
)
func main() {
var n zerodecimal.NullDecimal
// SQL NULL scans to Valid == false without an error.
if err := n.Scan(nil); err != nil {
panic(err)
}
fmt.Println(n.Valid)
// Driver strings parse with the strict literal grammar.
if err := n.Scan("123.45"); err != nil {
panic(err)
}
fmt.Println(n.Valid, n.Decimal)
// Value renders the canonical string every database agrees on.
v, err := n.Value()
if err != nil {
panic(err)
}
fmt.Println(v)
}
Output: false true 123.45 123.45
func NewNullDecimal ¶
func NewNullDecimal(d Decimal) NullDecimal
NewNullDecimal returns a valid NullDecimal holding d.
func (NullDecimal) MarshalJSON ¶
func (n NullDecimal) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler: the JSON null literal when n is invalid, otherwise the double-quoted canonical string of the held Decimal. The null bytes are freshly allocated too — callers own MarshalJSON results and may mutate them.
func (NullDecimal) MarshalText ¶
func (n NullDecimal) MarshalText() ([]byte, error)
MarshalText implements encoding.TextMarshaler: the empty string when n is invalid (the conventional text rendering of NULL), otherwise the canonical bytes of the held Decimal.
func (*NullDecimal) Scan ¶
func (n *NullDecimal) Scan(src any) error
Scan implements sql.Scanner: a nil src (SQL NULL) clears n to the invalid zero NullDecimal without error; any other src follows Decimal.Scan, with a conversion error also clearing n — a NullDecimal never holds a stale value after a failed scan.
func (*NullDecimal) UnmarshalJSON ¶
func (n *NullDecimal) UnmarshalJSON(data []byte) error
UnmarshalJSON implements json.Unmarshaler: a literal null clears n to the invalid zero NullDecimal (unlike Decimal, where null is a no-op — here the type exists to record it); anything else must parse per Decimal.UnmarshalJSON and marks n valid. A parse error leaves n unchanged.
func (*NullDecimal) UnmarshalText ¶
func (n *NullDecimal) UnmarshalText(b []byte) error
UnmarshalText implements encoding.TextUnmarshaler: empty input clears n to the invalid zero NullDecimal; anything else must parse as a strict decimal literal and marks n valid. A parse error leaves n unchanged.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
gen
command
Command gen computes the reciprocal-division tables for package zerodecimal and writes them to tables.go in the package root.
|
Command gen computes the reciprocal-division tables for package zerodecimal and writes them to tables.go in the package root. |