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
- type Decimal
- func (d Decimal) Abs() Decimal
- func (d Decimal) Add(other Decimal) (Decimal, error)
- func (d Decimal) Cmp(other Decimal) int
- func (d Decimal) Div(divisor Decimal, scale int, mode RoundingMode) (Decimal, error)
- func (d Decimal) Fixed(scale int) (string, error)
- func (d Decimal) IsZero() bool
- func (d Decimal) MarshalText() ([]byte, error)
- func (d Decimal) Mul(other Decimal) (Decimal, error)
- func (d Decimal) Neg() Decimal
- func (d Decimal) Round(scale int, mode RoundingMode) (Decimal, error)
- func (d Decimal) Scale() int
- func (d Decimal) Sign() int
- func (d Decimal) String() string
- func (d Decimal) Sub(other Decimal) (Decimal, error)
- func (d *Decimal) UnmarshalJSON(data []byte) error
- func (d *Decimal) UnmarshalText(data []byte) error
- type RoundingMode
Constants ¶
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 Parse ¶
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 ¶
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 ¶
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) Cmp ¶
Cmp compares numeric values, returning -1, 0 or 1. It compares canonical digits directly and does not allocate.
func (Decimal) Div ¶
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 ¶
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) MarshalText ¶
MarshalText encodes the decimal's canonical string as text.
func (Decimal) Mul ¶
Mul returns an exact product or a digit-bound error. Division requires an explicit result scale and rounding mode; see Div.
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) String ¶
String returns the decimal's canonical string, including "0" for the zero value.
func (*Decimal) UnmarshalJSON ¶
UnmarshalJSON decodes a JSON decimal, replacing the receiver only on success.
func (*Decimal) UnmarshalText ¶
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.