Help us shape the future of Go! Take the 2026 Go Developer Survey

decimal

package
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package decimal provides immutable, comparable finite decimal values without floating-point conversion. Values have canonical numeric equality, not a retained display scale. Use explicit formatting at presentation boundaries.

Index

Constants

View Source
const MaxDigits = 4096

MaxDigits bounds input digits and arithmetic results. This is a framework resource bound, not a claim to cover PostgreSQL's entire numeric range.

Variables

This section is empty.

Functions

This section is empty.

Types

type Decimal

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

Decimal is an exact finite decimal. Its zero value is numeric zero. Private canonical storage makes == agree with numeric equality, including signed zero.

func FromInt64

func FromInt64(value int64) Decimal

FromInt64 converts an integer exactly, without a parse failure path.

func Max

func Max(first Decimal, others ...Decimal) Decimal

Max returns the numerically largest value, preferring the earliest on ties.

func Min

func Min(first Decimal, others ...Decimal) Decimal

Min returns the numerically smallest value, preferring the earliest on ties.

func Parse

func Parse(text string) (Decimal, error)

Parse accepts optional +/-, digits and an optional fractional part. Exponents, whitespace, NaN and infinity are rejected. At least one digit is required on each side of a decimal point. Leading/trailing zeros are normalized.

func Scaled

func Scaled(coefficient int64, scale int) (Decimal, error)

Scaled returns coefficient × 10^-scale exactly, for example Scaled(1234, 2) is 12.34. It is the exact bridge from integer minor units.

func Sum

func Sum(values ...Decimal) (Decimal, error)

Sum returns the exact total of values, or zero for no values. Intermediate values use one aligned coefficient; only the final result is digit bounded.

func (Decimal) Abs

func (d Decimal) Abs() Decimal

Abs returns the magnitude.

func (Decimal) Add

func (d Decimal) Add(other Decimal) (Decimal, error)

Add returns an exact sum or a digit-bound error. Receivers never change.

func (Decimal) Cmp

func (d Decimal) Cmp(other Decimal) int

Cmp compares numeric values, returning -1, 0 or 1. It compares canonical digits directly and does not allocate.

func (Decimal) Div

func (d Decimal) Div(divisor Decimal, scale int, mode RoundingMode) (Decimal, error)

Div returns d / divisor rounded to at most scale fractional digits with an explicit mode. Division by zero, an invalid mode or scale, and results beyond MaxDigits fail. Operands never change.

func (Decimal) Fixed

func (d Decimal) Fixed(scale int) (string, error)

Fixed formats the value with exactly scale fractional digits, padding zeros. It never rounds: values with more fractional digits fail, so callers choose a rounding mode with Round first.

func (Decimal) IsZero

func (d Decimal) IsZero() bool

IsZero reports whether the decimal is zero.

func (Decimal) MarshalText

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

MarshalText encodes the decimal's canonical string as text.

func (Decimal) Mul

func (d Decimal) Mul(other Decimal) (Decimal, error)

Mul returns an exact product or a digit-bound error. Division requires an explicit result scale and rounding mode; see Div.

func (Decimal) Neg

func (d Decimal) Neg() Decimal

Neg returns the additive inverse. Negating zero returns zero.

func (Decimal) Round

func (d Decimal) Round(scale int, mode RoundingMode) (Decimal, error)

Round returns the value with at most scale fractional digits. Values that already fit are returned unchanged; nothing is padded because display scale is not retained. Use Fixed to present a fixed number of digits.

func (Decimal) Scale

func (d Decimal) Scale() int

Scale reports the number of fractional digits in the canonical value.

func (Decimal) Sign

func (d Decimal) Sign() int

Sign returns -1, 0 or 1.

func (Decimal) String

func (d Decimal) String() string

String returns the decimal's canonical string, including "0" for the zero value.

func (Decimal) Sub

func (d Decimal) Sub(other Decimal) (Decimal, error)

Sub returns an exact difference or a digit-bound error.

func (*Decimal) UnmarshalJSON

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

UnmarshalJSON decodes a JSON decimal, replacing the receiver only on success.

func (*Decimal) UnmarshalText

func (d *Decimal) UnmarshalText(data []byte) error

UnmarshalText parses decimal text, replacing the receiver only on success.

type RoundingMode

type RoundingMode uint8

RoundingMode selects how discarded digits change a rounded result. The zero value is invalid, so every rounding or division call states its policy.

const (
	// HalfUp rounds to the nearest value; ties move away from zero.
	HalfUp RoundingMode = iota + 1
	// HalfEven rounds to the nearest value; ties choose the even neighbor.
	HalfEven
	// Down truncates toward zero.
	Down
	// Up moves any discarded nonzero digits away from zero.
	Up
	// Floor rounds toward negative infinity.
	Floor
	// Ceiling rounds toward positive infinity.
	Ceiling
)

func (RoundingMode) String

func (m RoundingMode) String() string

String returns the rounding mode name, or "invalid" for an unsupported mode.

func (RoundingMode) Validate

func (m RoundingMode) Validate() error

Validate checks that the rounding mode is supported.

Directories

Path Synopsis
Package money pairs exact decimal amounts with ISO 4217 currencies.
Package money pairs exact decimal amounts with ISO 4217 currencies.

Jump to

Keyboard shortcuts

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