Documentation
¶
Overview ¶
Package decimalx provides an immutable arbitrary-precision decimal type built on cockroachdb/apd, with SQL and JSON support.
Index ¶
- Constants
- func Ctx() *apd.Context
- type Decimal
- func ApplyBasisPoints(amount Decimal, bp int64) Decimal
- func DerefOr(p *Decimal, def Decimal) Decimal
- func FromMinorUnits(amount int64, precision int32) Decimal
- func GetMaxDecimalValue() Decimal
- func Max(a, b Decimal) Decimal
- func Min(a, b Decimal) Decimal
- func New(coeff int64, exponent int32) Decimal
- func NewFromBasisPoints(bp int64) Decimal
- func NewFromInt64(v int64) Decimal
- func NewFromString(s string) (Decimal, error)
- func Zero() Decimal
- func (v Decimal) Abs() Decimal
- func (v Decimal) Add(other Decimal) Decimal
- func (v Decimal) Cmp(other Decimal) int
- func (v Decimal) Div(other Decimal) Decimal
- func (v Decimal) Equal(other Decimal) bool
- func (v Decimal) GreaterThan(other Decimal) bool
- func (v Decimal) GreaterThanOrEqual(other Decimal) bool
- func (v Decimal) Inner() *apd.Decimal
- func (v Decimal) Int64() int64
- func (v Decimal) IsNegative() bool
- func (v Decimal) IsPositive() bool
- func (v Decimal) IsZero() bool
- func (v Decimal) LessThan(other Decimal) bool
- func (v Decimal) LessThanOrEqual(other Decimal) bool
- func (v Decimal) MarshalJSON() ([]byte, error)
- func (v Decimal) Mul(other Decimal) Decimal
- func (v Decimal) Neg() Decimal
- func (v Decimal) Ptr() *Decimal
- func (v *Decimal) Scan(src interface{}) error
- func (v Decimal) String() string
- func (v Decimal) Sub(other Decimal) Decimal
- func (v Decimal) ToMinorUnits(precision int32) int64
- func (v *Decimal) UnmarshalJSON(data []byte) error
- func (v Decimal) Value() (driver.Value, error)
Constants ¶
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 ¶
Types ¶
type Decimal ¶
type Decimal struct {
// contains filtered or unexported fields
}
Decimal wraps *apd.Decimal for precise decimal calculations.
func ApplyBasisPoints ¶
ApplyBasisPoints computes amount * bp / 10000.
func FromMinorUnits ¶
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 NewFromBasisPoints ¶
NewFromBasisPoints converts basis points to a decimal fraction. For example, 1500 bp becomes 0.15.
func NewFromInt64 ¶
NewFromInt64 creates a Decimal from an int64 value.
func NewFromString ¶
NewFromString parses a decimal string. Returns an error for invalid input.
func (Decimal) GreaterThan ¶
GreaterThan returns true when v > other.
func (Decimal) GreaterThanOrEqual ¶ added in v0.7.1
GreaterThanOrEqual returns true when v >= other.
func (Decimal) LessThanOrEqual ¶ added in v0.7.1
LessThanOrEqual returns true when v <= other.
func (Decimal) MarshalJSON ¶
MarshalJSON implements json.Marshaler.
func (Decimal) Ptr ¶ added in v0.7.1
Ptr returns a pointer to the Decimal value. Useful for nullable fields.
func (Decimal) ToMinorUnits ¶
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 ¶
UnmarshalJSON implements json.Unmarshaler.