decimalx

package module
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2026 License: Apache-2.0 Imports: 4 Imported by: 1

Documentation

Overview

Package decimalx provides an immutable arbitrary-precision decimal type built on cockroachdb/apd, with SQL and JSON support.

Index

Constants

View Source
const (
	// DecimalPrecision is the precision used for decimal calculations.
	DecimalPrecision = 9
	// NanoSize is the multiplier for converting decimal fractions to nano units.
	NanoSize = 1_000_000_000
	// MaxNanosValue is the maximum value for nanos (10^9 - 1).
	MaxNanosValue = 999999999
)

Precision constants.

Variables

This section is empty.

Functions

func Ctx

func Ctx() *apd.Context

Ctx returns the shared arithmetic context.

Types

type Decimal

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

Decimal wraps *apd.Decimal for precise decimal calculations.

func ApplyBasisPoints

func ApplyBasisPoints(amount Decimal, bp int64) Decimal

ApplyBasisPoints computes amount * bp / 10000.

func DerefOr added in v0.7.1

func DerefOr(p *Decimal, def Decimal) Decimal

DerefOr dereferences p, returning def if p is nil.

func FromMinorUnits

func FromMinorUnits(amount int64, precision int32) Decimal

FromMinorUnits converts a minor-unit integer to a Decimal. For example, 12345 with precision 2 returns 123.45.

func GetMaxDecimalValue

func GetMaxDecimalValue() Decimal

GetMaxDecimalValue returns the maximum decimal value supported, matching the payment service's NUMERIC(29,9) column.

func Max added in v0.7.1

func Max(a, b Decimal) Decimal

Max returns the larger of v and other.

func Min added in v0.7.1

func Min(a, b Decimal) Decimal

Min returns the smaller of v and other.

func New

func New(coeff int64, exponent int32) Decimal

New creates a Decimal from a coefficient and exponent (coefficient * 10^exponent).

func NewFromBasisPoints

func NewFromBasisPoints(bp int64) Decimal

NewFromBasisPoints converts basis points to a decimal fraction. For example, 1500 bp becomes 0.15.

func NewFromInt64

func NewFromInt64(v int64) Decimal

NewFromInt64 creates a Decimal from an int64 value.

func NewFromString

func NewFromString(s string) (Decimal, error)

NewFromString parses a decimal string. Returns an error for invalid input.

func Zero

func Zero() Decimal

Zero returns a Decimal representing 0.

func (Decimal) Abs added in v0.7.1

func (v Decimal) Abs() Decimal

Abs returns the absolute value of v.

func (Decimal) Add

func (v Decimal) Add(other Decimal) Decimal

Add returns v + other.

func (Decimal) Cmp

func (v Decimal) Cmp(other Decimal) int

Cmp compares v and other: -1 if v < other, 0 if equal, +1 if v > other.

func (Decimal) Div

func (v Decimal) Div(other Decimal) Decimal

Div returns v / other. Panics on division by zero.

func (Decimal) Equal

func (v Decimal) Equal(other Decimal) bool

Equal returns true when v == other.

func (Decimal) GreaterThan

func (v Decimal) GreaterThan(other Decimal) bool

GreaterThan returns true when v > other.

func (Decimal) GreaterThanOrEqual added in v0.7.1

func (v Decimal) GreaterThanOrEqual(other Decimal) bool

GreaterThanOrEqual returns true when v >= other.

func (Decimal) Inner

func (v Decimal) Inner() *apd.Decimal

Inner returns the underlying *apd.Decimal for interop with other packages.

func (Decimal) Int64

func (v Decimal) Int64() int64

Int64 truncates the decimal and returns the integer part.

func (Decimal) IsNegative

func (v Decimal) IsNegative() bool

IsNegative returns true when v < 0.

func (Decimal) IsPositive

func (v Decimal) IsPositive() bool

IsPositive returns true when v > 0.

func (Decimal) IsZero

func (v Decimal) IsZero() bool

IsZero returns true when v == 0.

func (Decimal) LessThan

func (v Decimal) LessThan(other Decimal) bool

LessThan returns true when v < other.

func (Decimal) LessThanOrEqual added in v0.7.1

func (v Decimal) LessThanOrEqual(other Decimal) bool

LessThanOrEqual returns true when v <= other.

func (Decimal) MarshalJSON

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

MarshalJSON implements json.Marshaler.

func (Decimal) Mul

func (v Decimal) Mul(other Decimal) Decimal

Mul returns v * other.

func (Decimal) Neg

func (v Decimal) Neg() Decimal

Neg returns -v.

func (Decimal) Ptr added in v0.7.1

func (v Decimal) Ptr() *Decimal

Ptr returns a pointer to the Decimal value. Useful for nullable fields.

func (*Decimal) Scan

func (v *Decimal) Scan(src interface{}) error

Scan implements database/sql.Scanner.

func (Decimal) String

func (v Decimal) String() string

String returns the decimal as a plain string.

func (Decimal) Sub

func (v Decimal) Sub(other Decimal) Decimal

Sub returns v - other.

func (Decimal) ToMinorUnits

func (v Decimal) ToMinorUnits(precision int32) int64

ToMinorUnits converts the decimal to the smallest currency unit for the given precision. For example, 123.45 with precision 2 returns 12345.

func (*Decimal) UnmarshalJSON

func (v *Decimal) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler.

func (Decimal) Value

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

Value implements database/sql/driver.Valuer.

Jump to

Keyboard shortcuts

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