CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/golang-github-com-shopspring--decimal

Arbitrary-precision fixed-point decimal numbers for Go, avoiding floating-point precision issues with support for arithmetic, rounding, serialization, and database integration.

Overview
Eval results
Files

comparison.mddocs/api/

Comparison and Inspection

Methods for comparing Decimal values and inspecting their properties. Never use Go's == or != operators directly on Decimal values — always use .Equal() or .Cmp().

Capabilities

Ordering Comparison

// Cmp compares the numbers represented by d and d2.
// Returns:
//   -1 if d <  d2
//    0 if d == d2
//   +1 if d >  d2
func (d Decimal) Cmp(d2 Decimal) int

// Compare is an alias for Cmp.
// Returns -1, 0, or +1 for d < d2, d == d2, d > d2.
func (d Decimal) Compare(d2 Decimal) int

Usage:

a := decimal.NewFromFloat(1.5)
b := decimal.NewFromFloat(2.5)

a.Cmp(b)     // -1 (a < b)
b.Cmp(a)     // +1 (b > a)
a.Cmp(a)     // 0  (equal)

Equality

// Equal returns whether the numbers represented by d and d2 are equal.
func (d Decimal) Equal(d2 Decimal) bool

// Equals is deprecated; use Equal instead.
func (d Decimal) Equals(d2 Decimal) bool

Usage:

a := decimal.NewFromFloat(1.5)
b := decimal.NewFromFloat(1.50)  // same value, potentially different internal rep

a.Equal(b)   // true
a.Equal(decimal.Zero)  // false

Relational Operators

// GreaterThan returns true if d > d2.
func (d Decimal) GreaterThan(d2 Decimal) bool

// GreaterThanOrEqual returns true if d >= d2.
func (d Decimal) GreaterThanOrEqual(d2 Decimal) bool

// LessThan returns true if d < d2.
func (d Decimal) LessThan(d2 Decimal) bool

// LessThanOrEqual returns true if d <= d2.
func (d Decimal) LessThanOrEqual(d2 Decimal) bool

Usage:

a := decimal.NewFromFloat(1.5)
b := decimal.NewFromFloat(2.5)

a.GreaterThan(b)          // false
b.GreaterThan(a)          // true
a.GreaterThanOrEqual(a)   // true
a.LessThan(b)             // true
a.LessThanOrEqual(b)      // true
a.LessThanOrEqual(a)      // true

Sign and Zero Inspection

// Sign returns:
//   -1 if d <  0
//    0 if d == 0
//   +1 if d >  0
func (d Decimal) Sign() int

// IsZero returns true if d == 0.
func (d Decimal) IsZero() bool

// IsPositive returns true if d > 0.
func (d Decimal) IsPositive() bool

// IsNegative returns true if d < 0.
func (d Decimal) IsNegative() bool

// IsInteger returns true if d has no fractional part (is a whole number).
func (d Decimal) IsInteger() bool

Usage:

pos := decimal.NewFromFloat(3.14)
neg := decimal.NewFromFloat(-3.14)
zero := decimal.NewFromFloat(0)
whole := decimal.NewFromFloat(5.0)
frac := decimal.NewFromFloat(5.5)

pos.Sign()       // +1
neg.Sign()       // -1
zero.Sign()      // 0

zero.IsZero()    // true
pos.IsZero()     // false

pos.IsPositive() // true
neg.IsPositive() // false

neg.IsNegative() // true
pos.IsNegative() // false

whole.IsInteger() // true
frac.IsInteger()  // false

Aggregate Functions

Package-level functions operating on multiple Decimal values.

// Sum returns the combined total of all provided Decimals.
// Requires at least one argument (first is mandatory, rest is variadic).
func Sum(first Decimal, rest ...Decimal) Decimal

// Avg returns the average value of all provided Decimals.
// Requires at least one argument.
func Avg(first Decimal, rest ...Decimal) Decimal

// Max returns the largest Decimal from the arguments.
// Requires at least one argument.
// For slices: Max(arr[0], arr[1:]...)
func Max(first Decimal, rest ...Decimal) Decimal

// Min returns the smallest Decimal from the arguments.
// Requires at least one argument.
// For slices: Min(arr[0], arr[1:]...)
func Min(first Decimal, rest ...Decimal) Decimal

Usage:

a := decimal.NewFromFloat(1.0)
b := decimal.NewFromFloat(2.0)
c := decimal.NewFromFloat(3.0)

decimal.Sum(a, b, c)   // 6.0
decimal.Avg(a, b, c)   // 2.0
decimal.Max(a, b, c)   // 3.0
decimal.Min(a, b, c)   // 1.0

// Using a slice
values := []decimal.Decimal{a, b, c}
decimal.Max(values[0], values[1:]...)
decimal.Sum(values[0], values[1:]...)

Rescale Pair

// RescalePair rescales two decimals to a common exponent
// (the minimum/most-precise exponent of both).
func RescalePair(d1 Decimal, d2 Decimal) (Decimal, Decimal)

Usage:

d1 := decimal.NewFromFloat(1.5)    // internally exp=-1
d2 := decimal.NewFromFloat(1.500)  // internally exp=-3
r1, r2 := decimal.RescalePair(d1, d2)
// r1 = 1.500, r2 = 1.500 (both at exp=-3)

Install with Tessl CLI

npx tessl i tessl/golang-github-com-shopspring--decimal@1.4.1

docs

api

arithmetic.md

comparison.md

configuration.md

constructors.md

conversion.md

null-decimal.md

rounding.md

serialization.md

index.md

README.md

tile.json