Documentation
¶
Overview ¶
Package decimal provides an immutable arbitrary-precision decimal type built on top of math/big.Int.
Decimal values keep both unscaled integer digits and decimal precision, which makes the package suitable for financial and accounting scenarios that require deterministic base-10 behavior.
Concurrency ¶
A Decimal value is safe for concurrent read access by multiple goroutines provided that no goroutine reassigns the variable holding it. The type is designed around immutable semantics: arithmetic and inspection methods have value receivers (for example Add, Sub, Mul, Quo, Cmp, Sign, String, StringWithTrailingZeros, IntPart, Precision, IsZero, MarshalJSON, MarshalText, MarshalBinary) and return new Decimal values without mutating the receiver, so invoking them concurrently on the same Decimal is safe.
Methods with pointer receivers mutate the receiver and therefore require external synchronization whenever the same *Decimal may be accessed from more than one goroutine. These include the decoding entry points used by the standard library and popular frameworks: Scan, UnmarshalJSON, UnmarshalYAML, UnmarshalText, UnmarshalBinary, and UnmarshalParam.
Accessors that expose the underlying math/big types (for example BigInt and BigRat) return defensive copies rather than the internal state, so the returned *big.Int or *big.Rat may be read or mutated by the caller without affecting other goroutines that share the original Decimal.
The package-level constants Zero, One, Ten, and Hundred are intended to be treated as read-only singletons; do not pass them to APIs that would mutate a Decimal in place.
Index ¶
- Constants
- Variables
- func Between(v, lower, upper Decimal) bool
- func DefaultGoPlaygroundValidatorTranslationMessages(locale string) map[string]string
- func MostSignificantBit(x *big.Int) uint
- func RegisterGoPlaygroundValidator(v *validator.Validate) error
- func RegisterGoPlaygroundValidatorTranslations(v *validator.Validate, trans ut.Translator) error
- func RegisterGoPlaygroundValidatorTranslationsWithMessages(v *validator.Validate, trans ut.Translator, messages map[string]string) error
- func TranslateGoPlaygroundValidationErrors(err error, trans ut.Translator) []string
- type Decimal
- func Max(a, b Decimal) Decimal
- func Min(a, b Decimal) Decimal
- func MustFromString(str string) Decimal
- func New(value int64) Decimal
- func NewFromBigInt(value *big.Int) Decimal
- func NewFromBigIntWithPrec(value *big.Int, precision int) Decimal
- func NewFromBigRat(value *big.Rat) (Decimal, error)
- func NewFromBigRatWithPrec(value *big.Rat, prec int, roundingMode RoundingMode) (Decimal, error)
- func NewFromDecimal(d Decimal) Decimal
- func NewFromFloat32(value float32) Decimal
- func NewFromFloat64(value float64) Decimal
- func NewFromInt(value int) Decimal
- func NewFromInt64(value int64, precision int) Decimal
- func NewFromString(str string) (d Decimal, err error)
- func NewFromUint64(value uint64, precision int) Decimal
- func NewFromUintWithAppendPrec(value uint64, prec int) Decimal
- func NewWithAppendPrec(value int64, prec int) Decimal
- func NewWithPrec(value int64, prec int) Decimal
- func (d Decimal) Abs() Decimal
- func (d Decimal) Add(d2 Decimal) Decimal
- func (d Decimal) AddRaw(i int64) Decimal
- func (d Decimal) ApproxRoot(root int64) (Decimal, error)
- func (d Decimal) ApproxRootWithPrec(root int64, prec int) (Decimal, error)
- func (d Decimal) BigInt() *big.Int
- func (d Decimal) BigRat() *big.Rat
- func (d Decimal) BitLen() int
- func (d Decimal) Ceil() Decimal
- func (d Decimal) CeilWithPrec(prec int) Decimal
- func (d Decimal) Clone() Decimal
- func (d Decimal) Cmp(d2 Decimal) int
- func (d Decimal) Equal(d2 Decimal) bool
- func (d Decimal) Exp() (Decimal, error)
- func (d Decimal) ExpWithPrec(prec int) (Decimal, error)
- func (d Decimal) Float32() (float32, bool)
- func (d Decimal) Float64() (float64, bool)
- func (d Decimal) Floor() Decimal
- func (d Decimal) FloorWithPrec(prec int) Decimal
- func (d Decimal) Format(f fmt.State, verb rune)
- func (d Decimal) FormatWithSeparators(thousands, decimal rune) string
- func (d Decimal) GT(d2 Decimal) bool
- func (d Decimal) GTE(d2 Decimal) bool
- func (d Decimal) HasFraction() bool
- func (d Decimal) Int64() (int64, bool)
- func (d Decimal) IntPart() *big.Int
- func (d Decimal) IsInteger() bool
- func (d Decimal) IsNegative() bool
- func (d Decimal) IsNil() bool
- func (d Decimal) IsNotZero() bool
- func (d Decimal) IsPositive() bool
- func (d Decimal) IsZero() bool
- func (d Decimal) LT(d2 Decimal) bool
- func (d Decimal) LTE(d2 Decimal) bool
- func (d Decimal) Ln() (Decimal, error)
- func (d Decimal) LnWithPrec(prec int) (Decimal, error)
- func (d Decimal) Log2() Decimal
- func (d Decimal) Log10() (Decimal, error)
- func (d Decimal) Log10WithPrec(prec int) (Decimal, error)
- func (d Decimal) Marshal() ([]byte, error)
- func (d Decimal) MarshalBinary() (data []byte, err error)
- func (d Decimal) MarshalJSON() ([]byte, error)
- func (d Decimal) MarshalText() ([]byte, error)
- func (d Decimal) MarshalTo(data []byte) (n int, err error)
- func (d Decimal) MarshalXML(e *xml.Encoder, start xml.StartElement) error
- func (d Decimal) MarshalXMLAttr(name xml.Name) (xml.Attr, error)
- func (d Decimal) MarshalYAML() (any, error)
- func (d Decimal) Max(d2 Decimal) Decimal
- func (d Decimal) Min(d2 Decimal) Decimal
- func (d Decimal) Mod(d2 Decimal) Decimal
- func (d Decimal) Mul(d2 Decimal, roundingMode RoundingMode) Decimal
- func (d Decimal) Mul2(d2 Decimal) Decimaldeprecated
- func (d Decimal) MulDown(d2 Decimal) Decimal
- func (d Decimal) MulExact(d2 Decimal) Decimal
- func (d Decimal) MustNonNegative() Decimal
- func (d Decimal) Neg() Decimal
- func (d Decimal) NotEqual(d2 Decimal) bool
- func (d Decimal) Power(power int64) Decimal
- func (d Decimal) Precision() int
- func (d Decimal) Quo(d2 Decimal, roundingMode RoundingMode) Decimal
- func (d Decimal) QuoDown(d2 Decimal) Decimal
- func (d Decimal) QuoRem(d2 Decimal) (Decimal, Decimal)
- func (d Decimal) QuoWithPrec(d2 Decimal, prec int, roundingMode RoundingMode) Decimal
- func (d Decimal) Remainder() (intPart *big.Int, fractionPart *big.Int)
- func (d Decimal) Rescale(prec int, roundingMode RoundingMode) Decimal
- func (d Decimal) RescaleDown(prec int) Decimal
- func (d Decimal) Round() Decimal
- func (d Decimal) RoundWithPrec(prec int) Decimal
- func (d Decimal) SafeAdd(d2 Decimal) Decimal
- func (d Decimal) SafeSub(d2 Decimal) Decimal
- func (d *Decimal) Scan(value any) error
- func (d Decimal) Shift(places int) Decimal
- func (d Decimal) Sign() int
- func (d Decimal) SignificantFigures(figures int, roundingMode RoundingMode) Decimal
- func (d Decimal) Size() int
- func (d Decimal) Sqrt() (Decimal, error)
- func (d Decimal) SqrtWithPrec(prec int) (Decimal, error)
- func (d Decimal) String() string
- func (d Decimal) StringWithTrailingZeros() string
- func (d Decimal) StripTrailingZeros() Decimal
- func (d Decimal) Sub(d2 Decimal) Decimal
- func (d Decimal) SubRaw(i int64) Decimal
- func (d Decimal) Truncate() Decimal
- func (d Decimal) TruncateWithPrec(prec int) Decimal
- func (d Decimal) Uint64() (uint64, bool)
- func (d *Decimal) Unmarshal(data []byte) error
- func (d *Decimal) UnmarshalBinary(data []byte) error
- func (d *Decimal) UnmarshalJSON(bz []byte) error
- func (d *Decimal) UnmarshalParam(param string) error
- func (d *Decimal) UnmarshalText(text []byte) error
- func (d *Decimal) UnmarshalXML(dec *xml.Decoder, start xml.StartElement) error
- func (d *Decimal) UnmarshalXMLAttr(attr xml.Attr) error
- func (d *Decimal) UnmarshalYAML(unmarshal func(any) error) error
- func (d Decimal) Value() (driver.Value, error)
- type NullDecimal
- func (n NullDecimal) MarshalJSON() ([]byte, error)
- func (n NullDecimal) MarshalText() ([]byte, error)
- func (n NullDecimal) MarshalXML(e *xml.Encoder, start xml.StartElement) error
- func (n NullDecimal) MarshalXMLAttr(name xml.Name) (xml.Attr, error)
- func (n NullDecimal) MarshalYAML() (any, error)
- func (n *NullDecimal) Scan(value any) error
- func (n NullDecimal) String() string
- func (n *NullDecimal) UnmarshalJSON(data []byte) error
- func (n *NullDecimal) UnmarshalParam(param string) error
- func (n *NullDecimal) UnmarshalText(text []byte) error
- func (n *NullDecimal) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error
- func (n *NullDecimal) UnmarshalXMLAttr(attr xml.Attr) error
- func (n *NullDecimal) UnmarshalYAML(unmarshal func(any) error) error
- func (n NullDecimal) Value() (driver.Value, error)
- type RoundingMode
Examples ¶
- Between
- Decimal.Abs
- Decimal.Add
- Decimal.Add (DifferentPrecision)
- Decimal.AddRaw
- Decimal.ApproxRoot
- Decimal.ApproxRootWithPrec
- Decimal.BigInt
- Decimal.BigRat
- Decimal.BitLen
- Decimal.Ceil
- Decimal.CeilWithPrec
- Decimal.Clone
- Decimal.Cmp
- Decimal.Equal
- Decimal.Exp
- Decimal.ExpWithPrec
- Decimal.Float32
- Decimal.Float64
- Decimal.Floor
- Decimal.FloorWithPrec
- Decimal.Format
- Decimal.FormatWithSeparators
- Decimal.GT
- Decimal.GTE
- Decimal.HasFraction
- Decimal.Int64
- Decimal.IntPart
- Decimal.IsInteger
- Decimal.IsNegative
- Decimal.IsNil
- Decimal.IsNotZero
- Decimal.IsPositive
- Decimal.IsZero
- Decimal.LT
- Decimal.LTE
- Decimal.Ln
- Decimal.Log2
- Decimal.Log10
- Decimal.Log10WithPrec
- Decimal.Marshal
- Decimal.MarshalBinary
- Decimal.MarshalJSON
- Decimal.MarshalText
- Decimal.MarshalTo
- Decimal.MarshalXML
- Decimal.MarshalYAML
- Decimal.Max
- Decimal.Min
- Decimal.Mod
- Decimal.Mul
- Decimal.Mul (DifferentPrecision)
- Decimal.MulDown
- Decimal.MulExact
- Decimal.MustNonNegative
- Decimal.Neg
- Decimal.NotEqual
- Decimal.Power
- Decimal.Precision
- Decimal.Quo
- Decimal.Quo (DifferentPrecision)
- Decimal.QuoDown
- Decimal.QuoRem
- Decimal.QuoWithPrec
- Decimal.Remainder
- Decimal.Rescale
- Decimal.RescaleDown
- Decimal.Round
- Decimal.RoundWithPrec
- Decimal.SafeAdd
- Decimal.SafeSub
- Decimal.Scan
- Decimal.Shift
- Decimal.Sign
- Decimal.SignificantFigures
- Decimal.Size
- Decimal.Sqrt
- Decimal.SqrtWithPrec
- Decimal.String
- Decimal.StringWithTrailingZeros
- Decimal.StripTrailingZeros
- Decimal.Sub
- Decimal.Sub (DifferentPrecision)
- Decimal.SubRaw
- Decimal.Truncate
- Decimal.TruncateWithPrec
- Decimal.Uint64
- Decimal.Unmarshal
- Decimal.UnmarshalBinary
- Decimal.UnmarshalJSON
- Decimal.UnmarshalText
- Decimal.UnmarshalYAML
- Decimal.Value
- Max
- Min
- MostSignificantBit
- MustFromString
- New
- NewFromBigInt
- NewFromBigIntWithPrec
- NewFromBigRat
- NewFromBigRatWithPrec
- NewFromDecimal
- NewFromFloat32
- NewFromFloat64
- NewFromInt
- NewFromInt64
- NewFromString
- NewFromString (Negative)
- NewFromString (ScientificNotation)
- NewFromUint64
- NewFromUintWithAppendPrec
- NewWithAppendPrec
- NewWithPrec
- NullDecimal
- NullDecimal.MarshalJSON
- NullDecimal.UnmarshalJSON
- RegisterGoPlaygroundValidator
Constants ¶
const (
PrecisionFixedSize = 4
)
Variables ¶
var ( // Zero is the decimal zero value. Zero = New(0) // One is the decimal one value. One = New(1) // Ten is the decimal ten value. Ten = New(10) // Hundred is the decimal one hundred value. Hundred = New(100) )
var ( // ErrOverflow indicates a conversion overflowed the target numeric type, // such as int64, uint64, or float. ErrOverflow = errors.New("decimal: overflow") // ErrDivideByZero indicates an attempted division by zero in Quo, // QuoRem, or Mod. ErrDivideByZero = errors.New("decimal: division by zero") // ErrInvalidPrecision indicates a negative precision was supplied where // a non-negative value is required. ErrInvalidPrecision = errors.New("decimal: invalid precision") // ErrInvalidFormat indicates NewFromString failed to parse the input. ErrInvalidFormat = errors.New("decimal: invalid format") // ErrNegativeRoot indicates an attempt to take an even root of a // negative value in Sqrt or ApproxRoot. ErrNegativeRoot = errors.New("decimal: negative value for even root") // ErrInvalidRoot indicates a root value that is not strictly positive // was supplied to ApproxRoot. ErrInvalidRoot = errors.New("decimal: invalid root") // ErrInvalidLog indicates an attempt to take the logarithm of a // non-positive value. ErrInvalidLog = errors.New("decimal: log of non-positive value") // ErrRoundUnnecessary indicates rounding was required but // RoundUnnecessary was specified. ErrRoundUnnecessary = errors.New("decimal: rounding is necessary but RoundUnnecessary specified") // ErrUnmarshal indicates an unmarshal operation failed to parse input // into a Decimal. ErrUnmarshal = errors.New("decimal: unmarshal failed") // ErrInvalidArgument indicates an invalid argument was supplied to a // package-level function, typically during setup (for example a nil // validator or a missing required translation message). ErrInvalidArgument = errors.New("decimal: invalid argument") )
Sentinel errors returned by the decimal package.
These errors are wrapped by the package's functions so that callers may use errors.Is to identify a specific failure category without pattern matching on the error message.
Functions ¶
func Between ¶
Between reports whether v is within the inclusive range [lower, upper].
Example ¶
fmt.Println(Between(New(5), New(1), New(10)))
Output: true
func DefaultGoPlaygroundValidatorTranslationMessages ¶ added in v0.2.0
DefaultGoPlaygroundValidatorTranslationMessages returns built-in translation templates for Decimal validation tags by locale (with English fallback).
func MostSignificantBit ¶
MostSignificantBit returns the index of the most significant set bit in x. It returns 0 for x == 0 and panics if x < 0.
Example ¶
fmt.Println(MostSignificantBit(big.NewInt(16)))
Output: 4
func RegisterGoPlaygroundValidator ¶ added in v0.2.0
RegisterGoPlaygroundValidator registers Decimal-specific validation tags for go-playground/validator.
Registered tags:
- decimal_required
- decimal_eq
- decimal_ne
- decimal_gt
- decimal_gte
- decimal_lt
- decimal_lte
- decimal_between (param: "min~max" — tilde-separated bounds, inclusive; min must be <= max)
- decimal_positive
- decimal_negative
- decimal_nonzero
- decimal_max_precision (param: non-negative integer, max number of decimal places (scale); see validateDecimalMaxPrecision)
Use built-in omitempty as usual.
Validator tag parameters must be compile-time constants. Passing malformed parameters (non-numeric limits, unparseable decimal values, min > max for decimal_between, negative decimal_max_precision) causes panics at validation time — do not splice untrusted input into struct tags.
Calling this function multiple times on the same *validator.Validate is safe and idempotent: re-registration overwrites the previous handler for each tag.
Example ¶
v := validator.New()
_ = decimal.RegisterGoPlaygroundValidator(v)
type Req struct {
Price decimal.Decimal `validate:"decimal_required,decimal_positive,decimal_max_precision=2"`
Rate decimal.Decimal `validate:"decimal_between=0~1"`
}
good := Req{
Price: decimal.MustFromString("9.99"),
Rate: decimal.MustFromString("0.25"),
}
fmt.Println(v.Struct(good))
bad := Req{
Price: decimal.MustFromString("9.999"), // too many decimal places
Rate: decimal.MustFromString("0.25"),
}
err := v.Struct(bad)
if verr, ok := err.(validator.ValidationErrors); ok {
fmt.Println(verr[0].Tag())
}
Output: <nil> decimal_max_precision
func RegisterGoPlaygroundValidatorTranslations ¶ added in v0.2.0
func RegisterGoPlaygroundValidatorTranslations(v *validator.Validate, trans ut.Translator) error
RegisterGoPlaygroundValidatorTranslations registers friendly error messages for Decimal validator tags on the provided translator.
It selects built-in messages by trans.Locale(), with English fallback. Built-in locales: en, zh (Simplified), zh_Hant (Traditional Chinese, also matches zh_TW), ja, ko, fr, es, de, pt, pt_BR, ru, ar, hi.
func RegisterGoPlaygroundValidatorTranslationsWithMessages ¶ added in v0.2.0
func RegisterGoPlaygroundValidatorTranslationsWithMessages(v *validator.Validate, trans ut.Translator, messages map[string]string) error
RegisterGoPlaygroundValidatorTranslationsWithMessages registers friendly Decimal validator messages using caller-provided templates.
Any missing tag message falls back to English defaults.
func TranslateGoPlaygroundValidationErrors ¶ added in v0.2.0
func TranslateGoPlaygroundValidationErrors(err error, trans ut.Translator) []string
TranslateGoPlaygroundValidationErrors converts validator errors into friendly messages.
Types ¶
type Decimal ¶
type Decimal struct {
// contains filtered or unexported fields
}
Decimal represents a decimal number with arbitrary precision.
func MustFromString ¶
MustFromString returns a Decimal parsed from str and panics if parsing fails.
Example ¶
fmt.Println(MustFromString("12.34"))
Output: 12.34
func New ¶
New returns a Decimal created from value with precision 0.
Example ¶
fmt.Println(New(42))
Output: 42
func NewFromBigInt ¶
NewFromBigInt returns a Decimal created from value with precision 0.
Example ¶
fmt.Println(NewFromBigInt(big.NewInt(123)))
Output: 123
func NewFromBigIntWithPrec ¶
NewFromBigIntWithPrec returns a Decimal created from value with the given precision. It panics if precision is negative.
Example ¶
fmt.Println(NewFromBigIntWithPrec(big.NewInt(12345), 2))
Output: 123.45
func NewFromBigRat ¶
NewFromBigRat returns a Decimal converted from value.
It returns an error when value is nil or cannot be represented as a terminating decimal (for example 1/3).
Example ¶
d, err := NewFromBigRat(big.NewRat(7, 4)) fmt.Println(d, err == nil)
Output: 1.75 true
func NewFromBigRatWithPrec ¶
NewFromBigRatWithPrec returns a Decimal converted from value at precision prec.
The result is rounded according to roundingMode. It returns an error when value is nil and panics when prec is negative.
Example ¶
d, err := NewFromBigRatWithPrec(big.NewRat(1, 3), 2, RoundHalfEven) fmt.Println(d, err == nil)
Output: 0.33 true
func NewFromDecimal ¶ added in v0.3.0
NewFromDecimal returns a deep copy of d. It is provided for API symmetry with the other NewFrom* constructors and is equivalent to d.Clone().
Example ¶
src := MustFromString("3.14")
dst := NewFromDecimal(src)
fmt.Println(dst)
Output: 3.14
func NewFromFloat32 ¶
NewFromFloat32 returns a Decimal parsed from value.
Example ¶
fmt.Println(NewFromFloat32(12.34))
Output: 12.34
func NewFromFloat64 ¶
NewFromFloat64 returns a Decimal parsed from value.
Example ¶
fmt.Println(NewFromFloat64(12.34))
Output: 12.34
func NewFromInt ¶
NewFromInt returns a Decimal created from value with precision 0.
Example ¶
fmt.Println(NewFromInt(42))
Output: 42
func NewFromInt64 ¶
NewFromInt64 returns a Decimal created from value with the given precision. It panics if precision is negative.
Example ¶
fmt.Println(NewFromInt64(12345, 2))
Output: 123.45
func NewFromString ¶
NewFromString returns a Decimal parsed from str.
It accepts plain decimal values and scientific notation, and returns an error for empty or malformed input.
Example ¶
d, err := NewFromString("00123.4500")
if err != nil {
panic(err)
}
fmt.Println(d.String())
fmt.Println(d.StringWithTrailingZeros())
Output: 123.45 123.4500
Example (Negative) ¶
d, err := NewFromString("-00123.4500")
if err != nil {
panic(err)
}
fmt.Println(d.String())
fmt.Println(d.StringWithTrailingZeros())
Output: -123.45 -123.4500
Example (ScientificNotation) ¶
d1, err := NewFromString("1.23456e3")
if err != nil {
panic(err)
}
d2, err := NewFromString("-4.56E-2")
if err != nil {
panic(err)
}
fmt.Println(d1.String())
fmt.Println(d2.String())
fmt.Println(d2.StringWithTrailingZeros())
Output: 1234.56 -0.0456 -0.0456
func NewFromUint64 ¶
NewFromUint64 returns a Decimal created from value with the given precision. It panics if precision is negative.
Example ¶
fmt.Println(NewFromUint64(12345, 2))
Output: 123.45
func NewFromUintWithAppendPrec ¶
NewFromUintWithAppendPrec returns a Decimal created from value with prec trailing zeros appended. It panics if prec is negative.
Example ¶
fmt.Println(NewFromUintWithAppendPrec(12, 3).StringWithTrailingZeros())
Output: 12.000
func NewWithAppendPrec ¶
NewWithAppendPrec returns a Decimal created from value with prec trailing zeros appended. It panics if prec is negative.
Example ¶
fmt.Println(NewWithAppendPrec(12, 3).StringWithTrailingZeros())
Output: 12.000
func NewWithPrec ¶
NewWithPrec returns a Decimal created from value with the given precision. It panics if prec is negative.
Example ¶
fmt.Println(NewWithPrec(1234, 2))
Output: 12.34
func (Decimal) Abs ¶
Abs returns the absolute value of d.
Example ¶
fmt.Println(New(-1).Abs())
Output: 1
func (Decimal) Add ¶
Add returns d + d2, rescaled to the larger precision of the two values.
Example ¶
sum := MustFromString("1.20").Add(MustFromString("2.34"))
rounded := MustFromString("2.555").Rescale(2, RoundHalfEven)
fmt.Println(sum.String())
fmt.Println(rounded.String())
Output: 3.54 2.56
Example (DifferentPrecision) ¶
r := MustFromString("1.2").Add(MustFromString("0.030"))
fmt.Println(r.String(), r.Precision())
fmt.Println(r.StringWithTrailingZeros())
Output: 1.23 3 1.230
func (Decimal) AddRaw ¶
AddRaw returns d + i while preserving d's precision.
Example ¶
fmt.Println(MustFromString("1.23").AddRaw(1))
Output: 1.24
func (Decimal) ApproxRoot ¶
ApproxRoot returns an approximate integer-th root of d using iterative refinement.
Note: since v0.3.0 the output precision is max(d.Precision(), 30), which is a breaking change relative to v0.2.x where the result matched the receiver precision exactly. Callers that need the previous behavior should use ApproxRootWithPrec(root, d.Precision()) explicitly.
It returns an error wrapping ErrInvalidRoot when root is not strictly positive, or ErrNegativeRoot when d is negative and root is even. The output precision is max(d.prec, defaultLogExpPrec).
Example ¶
v, err := MustFromString("3125.0000").ApproxRoot(5)
fmt.Println(v, err == nil)
Output: 5 true
func (Decimal) ApproxRootWithPrec ¶ added in v0.3.0
ApproxRootWithPrec returns an approximate integer-th root of d rescaled to prec decimal places using RoundHalfEven.
Note: since v0.3.0 callers that pass prec smaller than 30 still observe a lifted working precision internally, but the final result is rescaled to the requested prec. Pass d.Precision() to reproduce the v0.2.x behavior of matching the receiver precision.
It returns an error wrapping ErrInvalidRoot when root is not strictly positive, or ErrNegativeRoot when d is negative and root is even. It panics if prec is negative.
Example ¶
v, _ := MustFromString("27").ApproxRootWithPrec(3, 6)
fmt.Println(v)
Output: 3
func (Decimal) BigInt ¶
BigInt returns a copy of the underlying big.Int value.
Example ¶
fmt.Println(MustFromString("12.34").BigInt())
Output: 1234
func (Decimal) BigRat ¶
BigRat returns d as an exact rational value.
Example ¶
fmt.Println(MustFromString("12.34").BigRat().RatString())
Output: 617/50
func (Decimal) BitLen ¶
BitLen returns the bit length of d's underlying integer representation.
Example ¶
fmt.Println(New(7).BitLen())
Output: 3
func (Decimal) Ceil ¶
Ceil returns the least integer value greater than or equal to d.
Example ¶
fmt.Println(MustFromString("-1.2").Ceil())
Output: -1
func (Decimal) CeilWithPrec ¶
CeilWithPrec returns d rounded toward positive infinity at the given precision. It panics if prec is negative.
Example ¶
fmt.Println(MustFromString("-1.239").CeilWithPrec(2))
Output: -1.23
func (Decimal) Clone ¶ added in v0.3.0
Clone returns a deep copy of d. The returned Decimal shares no mutable state with d, so mutating the underlying big.Int of either value will not affect the other.
Example ¶
original := MustFromString("42.5")
copy := original.Clone()
fmt.Println(copy.Equal(original))
Output: true
func (Decimal) Cmp ¶
Cmp compares d and d2 and returns:
-1 if d < d2 0 if d == d2 +1 if d > d2
Example ¶
fmt.Println(New(1).Cmp(New(2)))
Output: -1
func (Decimal) Equal ¶
Equal returns true if d and d2 are equal.
Example ¶
fmt.Println(New(1).Equal(New(1)))
Output: true
func (Decimal) Exp ¶ added in v0.3.0
Exp returns an approximate value of e raised to the power of d.
It evaluates the Taylor series e^x = sum_{n>=0} x^n / n! with argument reduction: e^x = (e^(x/2^k))^(2^k). The reduction shrinks |x| below 0.5, which guarantees rapid convergence. The iteration is bounded by maxIterations.
Example ¶
result, _ := MustFromString("0").Exp()
fmt.Println(result)
Output: 1
func (Decimal) ExpWithPrec ¶ added in v0.3.0
ExpWithPrec returns e^d rescaled to prec decimal places using RoundHalfEven.
It panics if prec is negative.
Example ¶
result, _ := MustFromString("1").ExpWithPrec(4)
fmt.Println(result)
Output: 2.7183
func (Decimal) Float32 ¶
Float32 returns the nearest float32 value for d and whether it is exact.
Example ¶
v, exact := MustFromString("0.5").Float32()
fmt.Println(v, exact)
Output: 0.5 true
func (Decimal) Float64 ¶
Float64 returns the nearest float64 value for d and whether it is exact.
Example ¶
v, exact := MustFromString("0.5").Float64()
fmt.Println(v, exact)
Output: 0.5 true
func (Decimal) Floor ¶
Floor returns the greatest integer value less than or equal to d.
Example ¶
fmt.Println(MustFromString("-1.2").Floor())
Output: -2
func (Decimal) FloorWithPrec ¶
FloorWithPrec returns d rounded toward negative infinity at the given precision. It panics if prec is negative.
Example ¶
fmt.Println(MustFromString("-1.239").FloorWithPrec(2))
Output: -1.24
func (Decimal) Format ¶ added in v0.3.0
Format implements fmt.Formatter, providing support for the common numeric verbs along with width, precision, and flag handling.
Supported verbs:
%v, %s the canonical string form (same as String)
%q the canonical string form wrapped in quotes
%d the integer value; only valid for integer Decimals. Non-integer
values produce the %!d(decimal.Decimal=<string>) error marker so
callers do not silently truncate fractional digits.
%f fixed-point notation; precision selects fractional digits
%e, %E scientific notation (default precision 6)
%g, %G the shorter of %e or %f for the given precision
%b binary representation of the unscaled big.Int with a scale tag
Width, precision, '-', '+', ' ', and '0' flags are honored where they make sense for the chosen verb.
Example ¶
d := MustFromString("1234.5678")
fmt.Printf("%s\n", d)
fmt.Printf("%.2f\n", d)
fmt.Printf("%e\n", d)
Output: 1234.5678 1234.57 1.234568e+03
func (Decimal) FormatWithSeparators ¶ added in v0.3.0
FormatWithSeparators returns d formatted with the supplied thousands and decimal separators. A thousands value of 0 disables grouping. A decimal value of 0 is treated as a zero rune and falls back to '.' so the output is always a syntactically valid decimal literal. The fractional part keeps its full precision as produced by StringWithTrailingZeros.
Example ¶
d := MustFromString("1234567.89")
fmt.Println(d.FormatWithSeparators(',', '.'))
fmt.Println(d.FormatWithSeparators('.', ','))
fmt.Println(d.FormatWithSeparators(' ', '.'))
Output: 1,234,567.89 1.234.567,89 1 234 567.89
func (Decimal) GT ¶
GT returns true if d is greater than d2.
Example ¶
fmt.Println(New(2).GT(New(1)))
Output: true
func (Decimal) GTE ¶
GTE returns true if d is greater than or equal to d2.
Example ¶
fmt.Println(New(2).GTE(New(2)))
Output: true
func (Decimal) HasFraction ¶
HasFraction returns true if d has a fractional part.
Example ¶
fmt.Println(MustFromString("1.25").HasFraction())
Output: true
func (Decimal) Int64 ¶
Int64 returns d as an int64 if it is an exact integer in range.
Example ¶
v, ok := MustFromString("42.0").Int64()
fmt.Println(v, ok)
Output: 42 true
func (Decimal) IntPart ¶
IntPart returns the integer part of d.
Example ¶
fmt.Println(MustFromString("12.34").IntPart())
Output: 12
func (Decimal) IsInteger ¶
IsInteger returns true if d has no fractional part.
Example ¶
fmt.Println(MustFromString("1.000").IsInteger())
Output: true
func (Decimal) IsNegative ¶
IsNegative returns true if d is negative.
Example ¶
fmt.Println(New(-1).IsNegative())
Output: true
func (Decimal) IsNil ¶
IsNil returns true if d has no underlying value.
Example ¶
var d Decimal fmt.Println(d.IsNil())
Output: true
func (Decimal) IsNotZero ¶
IsNotZero returns true if d is not zero.
Example ¶
fmt.Println(New(1).IsNotZero())
Output: true
func (Decimal) IsPositive ¶
IsPositive returns true if d is positive.
Example ¶
fmt.Println(New(1).IsPositive())
Output: true
func (Decimal) IsZero ¶
IsZero returns true if d is zero or nil.
Example ¶
fmt.Println(New(0).IsZero())
Output: true
func (Decimal) LT ¶
LT returns true if d is less than d2.
Example ¶
fmt.Println(New(1).LT(New(2)))
Output: true
func (Decimal) LTE ¶
LTE returns true if d is less than or equal to d2.
Example ¶
fmt.Println(New(1).LTE(New(1)))
Output: true
func (Decimal) Ln ¶ added in v0.3.0
Ln returns an approximate natural logarithm of d.
It returns an error wrapping ErrInvalidLog when d is not strictly positive. Internally Ln is computed as Log2(d) * ln(2) using a pre-computed high-precision constant for ln(2).
Example ¶
x := MustFromString("2.718281828459045235360287")
result, _ := x.Ln()
fmt.Println(result.Rescale(6, RoundHalfEven))
Output: 1
func (Decimal) LnWithPrec ¶ added in v0.3.0
LnWithPrec returns an approximate natural logarithm of d rescaled to prec decimal places using RoundHalfEven.
It returns an error wrapping ErrInvalidLog when d is not strictly positive. It panics if prec is negative.
func (Decimal) Log2 ¶
Log2 returns an approximate log base 2 of d via iterative refinement. The iteration is bounded by maxIterations. It panics if d is not greater than 0.
Example ¶
fmt.Println(New(8).Log2())
Output: 3
func (Decimal) Log10 ¶ added in v0.3.0
Log10 returns an approximate base-10 logarithm of d.
It returns an error wrapping ErrInvalidLog when d is not strictly positive. Internally Log10 is computed as Log2(d) / Log2(10) at the receiver's precision (bumped to defaultLogExpPrec when the receiver has fewer fractional digits). The result is rescaled to the receiver's precision so that callers receive output in a familiar scale.
Example ¶
x := MustFromString("100")
result, _ := x.Log10()
fmt.Println(result.Rescale(0, RoundHalfEven))
Output: 2
func (Decimal) Log10WithPrec ¶ added in v0.3.0
Log10WithPrec returns an approximate base-10 logarithm of d rescaled to prec decimal places using RoundHalfEven.
It returns an error wrapping ErrInvalidLog when d is not strictly positive. It panics if prec is negative.
Example ¶
result, _ := MustFromString("1000").Log10WithPrec(2)
fmt.Println(result)
Output: 3
func (Decimal) Marshal ¶
Marshal implements gogo-protobuf custom type marshaling via MarshalBinary.
Example ¶
bz, err := MustFromString("1.23").Marshal()
fmt.Println(len(bz) > 0 && err == nil)
Output: true
func (Decimal) MarshalBinary ¶
MarshalBinary implements encoding.BinaryMarshaler. The binary layout is 4 bytes of big-endian precision followed by big.Int Gob bytes. It strips trailing zeros before encoding and returns nil for an uninitialized value.
Example ¶
bz, err := MustFromString("1.23").MarshalBinary()
fmt.Println(len(bz) > 0 && err == nil)
Output: true
func (Decimal) MarshalJSON ¶
MarshalJSON implements json.Marshaler. It encodes a decimal as a JSON string and encodes an uninitialized value as null.
Example ¶
bz, _ := MustFromString("1.23").MarshalJSON()
fmt.Println(string(bz))
Output: "1.23"
func (Decimal) MarshalText ¶
MarshalText implements encoding.TextMarshaler by returning the decimal string form.
Example ¶
bz, _ := MustFromString("1.23").MarshalText()
fmt.Println(string(bz))
Output: 1.23
func (Decimal) MarshalTo ¶
MarshalTo implements gogo-protobuf custom type marshaling into data.
Example ¶
d := MustFromString("1.23")
buf := make([]byte, d.Size())
n, err := d.MarshalTo(buf)
fmt.Println(n > 0 && err == nil)
Output: true
func (Decimal) MarshalXML ¶ added in v0.3.0
MarshalXML implements xml.Marshaler. It encodes the decimal as character data using the canonical string form. An uninitialized Decimal is encoded as an empty element.
Example ¶
package main
import (
"bytes"
"encoding/xml"
"fmt"
"github.com/exc-works/decimal"
)
func main() {
type Item struct {
XMLName xml.Name `xml:"item"`
Amount decimal.Decimal `xml:"amount"`
}
var buf bytes.Buffer
enc := xml.NewEncoder(&buf)
_ = enc.Encode(Item{Amount: decimal.MustFromString("42.5")})
fmt.Println(buf.String())
}
Output: <item><amount>42.5</amount></item>
func (Decimal) MarshalXMLAttr ¶ added in v0.3.0
MarshalXMLAttr implements xml.MarshalerAttr. An uninitialized Decimal yields an attribute with an empty value.
func (Decimal) MarshalYAML ¶
MarshalYAML implements yaml.Marshaler by returning the decimal string form.
Example ¶
v, _ := MustFromString("1.23").MarshalYAML()
fmt.Println(v)
Output: 1.23
func (Decimal) Max ¶
Max returns the larger of d and d2.
Example ¶
fmt.Println(New(1).Max(New(2)))
Output: 2
func (Decimal) Min ¶
Min returns the smaller of d and d2.
Example ¶
fmt.Println(New(1).Min(New(2)))
Output: 1
func (Decimal) Mod ¶
Mod returns the same remainder component as QuoRem (truncated division). It panics if d2 is zero.
Example ¶
fmt.Println(MustFromString("-7").Mod(New(3)))
Output: -1
func (Decimal) Mul ¶
func (d Decimal) Mul(d2 Decimal, roundingMode RoundingMode) Decimal
Mul returns d * d2 rounded according to roundingMode.
Example ¶
fmt.Println(MustFromString("1.25").Mul(MustFromString("2.00"), RoundHalfEven))
Output: 2.5
Example (DifferentPrecision) ¶
r := MustFromString("1.234").Mul(MustFromString("2.5"), RoundHalfEven)
fmt.Println(r.String(), r.Precision())
Output: 3.085 3
func (Decimal) MulDown ¶
MulDown returns d * d2 rounded down.
Example ¶
fmt.Println(MustFromString("1.25").MulDown(MustFromString("2.00")))
Output: 2.5
func (Decimal) MulExact ¶
MulExact returns d * d2 without rounding, using the sum of input precisions.
Example ¶
fmt.Println(MustFromString("1.20").MulExact(MustFromString("2.30")).StringWithTrailingZeros())
Output: 2.7600
func (Decimal) MustNonNegative ¶
MustNonNegative returns d and panics if d is negative.
Example ¶
fmt.Println(New(1).MustNonNegative())
Output: 1
func (Decimal) Neg ¶
Neg returns the negated decimal.
Example ¶
fmt.Println(New(1).Neg())
Output: -1
func (Decimal) NotEqual ¶
NotEqual returns true if d and d2 are not equal.
Example ¶
fmt.Println(New(1).NotEqual(New(2)))
Output: true
func (Decimal) Power ¶
Power returns d raised to the given integer power.
Example ¶
fmt.Println(MustFromString("1.5").Power(3))
Output: 3.4
func (Decimal) Precision ¶
Precision returns the number of decimal places in d.
Example ¶
fmt.Println(MustFromString("1.23").Precision())
Output: 2
func (Decimal) Quo ¶
func (d Decimal) Quo(d2 Decimal, roundingMode RoundingMode) Decimal
Quo returns d / d2 rounded according to roundingMode. It panics if d2 is zero or roundingMode is invalid.
Example ¶
fmt.Println(New(7).Quo(New(2), RoundDown))
Output: 3
Example (DifferentPrecision) ¶
r := MustFromString("12.3").Quo(MustFromString("0.20"), RoundHalfEven)
fmt.Println(r.String(), r.Precision())
fmt.Println(r.StringWithTrailingZeros())
Output: 61.5 2 61.50
func (Decimal) QuoDown ¶
QuoDown returns d / d2 rounded down.
Example ¶
fmt.Println(New(7).QuoDown(New(2)))
Output: 3
func (Decimal) QuoRem ¶
QuoRem returns the quotient truncated toward zero and the corresponding remainder. It panics if d2 is zero.
Example ¶
q, r := MustFromString("-7").QuoRem(New(3))
fmt.Println(q, r)
Output: -2 -1
func (Decimal) QuoWithPrec ¶
func (d Decimal) QuoWithPrec(d2 Decimal, prec int, roundingMode RoundingMode) Decimal
QuoWithPrec returns d / d2 rounded to prec decimal places using roundingMode. It panics if prec is negative, d2 is zero, or roundingMode is invalid.
Example ¶
fmt.Println(New(1).QuoWithPrec(New(3), 6, RoundHalfEven))
Output: 0.333333
func (Decimal) Remainder ¶
Remainder returns the integer part and fractional part of d.
Example ¶
i, f := MustFromString("12.34").Remainder()
fmt.Println(i, f)
Output: 12 34
func (Decimal) Rescale ¶
func (d Decimal) Rescale(prec int, roundingMode RoundingMode) Decimal
Rescale returns d rescaled to prec decimal places using roundingMode. It panics if prec is negative or roundingMode is invalid.
Example ¶
d := MustFromString("7.5000").Rescale(2, RoundDown)
bz, err := d.MarshalJSON()
if err != nil {
panic(err)
}
fmt.Println(d.StringWithTrailingZeros())
fmt.Println(string(bz))
Output: 7.50 "7.5"
func (Decimal) RescaleDown ¶
RescaleDown returns d rescaled to prec decimal places using RoundDown. It panics if prec is negative.
Example ¶
fmt.Println(MustFromString("1.29").RescaleDown(1))
Output: 1.2
func (Decimal) Round ¶
Round returns d rounded to the nearest integer using RoundHalfEven.
Example ¶
fmt.Println(MustFromString("2.5").Round())
fmt.Println(MustFromString("3.5").Round())
Output: 2 4
func (Decimal) RoundWithPrec ¶
RoundWithPrec returns d rounded to the given precision using RoundHalfEven. It panics if prec is negative.
Example ¶
fmt.Println(MustFromString("1.245").RoundWithPrec(2))
Output: 1.24
func (Decimal) SafeAdd ¶
SafeAdd returns d + d2 and panics if the result is negative.
Example ¶
fmt.Println(New(2).SafeAdd(New(3)))
Output: 5
func (Decimal) SafeSub ¶
SafeSub returns d - d2 and panics if the result is negative.
Example ¶
fmt.Println(New(5).SafeSub(New(2)))
Output: 3
func (*Decimal) Scan ¶
Scan implements sql.Scanner. It accepts nil, float32, float64, int64, string, and []byte inputs, and updates d in place. Nil input resets d to its uninitialized state.
Example ¶
var d Decimal
_ = d.Scan("1.23")
fmt.Println(d)
Output: 1.23
func (Decimal) Shift ¶
Shift returns d multiplied by 10^places exactly.
Positive places shift the decimal point to the right; negative places shift it to the left.
Example ¶
fmt.Println(MustFromString("12.34").Shift(2))
fmt.Println(MustFromString("12.34").Shift(-3))
Output: 1234 0.01234
func (Decimal) Sign ¶
Sign returns:
-1 if d < 0 0 if d == 0 +1 if d > 0
Example ¶
fmt.Println(New(-1).Sign())
Output: -1
func (Decimal) SignificantFigures ¶
func (d Decimal) SignificantFigures(figures int, roundingMode RoundingMode) Decimal
SignificantFigures returns d rounded to figures significant figures. It may round within the fractional part or to tens/hundreds on the integer part. It panics if figures is not greater than 0.
Example ¶
fmt.Println(MustFromString("123.456").SignificantFigures(4, RoundHalfEven))
fmt.Println(MustFromString("123.456").SignificantFigures(3, RoundHalfEven))
fmt.Println(MustFromString("123.456").SignificantFigures(2, RoundHalfEven))
Output: 123.5 123 120
func (Decimal) Size ¶
Size implements gogo-protobuf custom type sizing based on Marshal output.
Example ¶
fmt.Println(MustFromString("1.23").Size() > 0)
Output: true
func (Decimal) Sqrt ¶
Sqrt returns an approximate square root of d.
Note: since v0.3.0 the output precision is max(d.Precision(), 30), which is a breaking change relative to v0.2.x where the result matched the receiver precision exactly. Callers that need the previous behavior should use SqrtWithPrec(d.Precision()) explicitly.
It returns an error wrapping ErrNegativeRoot when d is negative. The output precision is max(d.prec, defaultLogExpPrec) so that integer receivers still produce meaningful results.
Example ¶
v, err := MustFromString("16.0").Sqrt()
fmt.Println(v, err == nil)
Output: 4 true
func (Decimal) SqrtWithPrec ¶ added in v0.3.0
SqrtWithPrec returns an approximate square root of d rescaled to prec decimal places using RoundHalfEven.
Note: since v0.3.0 callers that pass prec smaller than 30 still observe a lifted working precision internally, but the final result is rescaled to the requested prec. Pass d.Precision() to reproduce the v0.2.x behavior of matching the receiver precision.
It returns an error wrapping ErrNegativeRoot when d is negative. It panics if prec is negative.
Example ¶
v, _ := MustFromString("2").SqrtWithPrec(10)
fmt.Println(v)
Output: 1.4142135624
func (Decimal) String ¶
String removes trailing zeros from the decimal representation.
Example ¶
fmt.Println(MustFromString("1.2300").String())
Output: 1.23
func (Decimal) StringWithTrailingZeros ¶
StringWithTrailingZeros returns the string with trailing zeros in the decimal representation.
Example ¶
fmt.Println(MustFromString("1.2300").StringWithTrailingZeros())
Output: 1.2300
func (Decimal) StripTrailingZeros ¶
StripTrailingZeros returns a Decimal which is numerically equal to this one but with any trailing zeros removed from the representation.
Example ¶
fmt.Println(MustFromString("1.2300").StripTrailingZeros())
Output: 1.23
func (Decimal) Sub ¶
Sub returns d - d2, rescaled to the larger precision of the two values.
Example ¶
fmt.Println(MustFromString("5.5").Sub(MustFromString("2.2")))
Output: 3.3
Example (DifferentPrecision) ¶
r := MustFromString("5.00").Sub(MustFromString("0.125"))
fmt.Println(r.String(), r.Precision())
Output: 4.875 3
func (Decimal) SubRaw ¶
SubRaw returns d - i while preserving d's precision.
Example ¶
fmt.Println(MustFromString("5.5").SubRaw(1))
Output: 5.4
func (Decimal) Truncate ¶
Truncate returns d rounded toward zero to an integer value.
Example ¶
fmt.Println(MustFromString("-1.9").Truncate())
Output: -1
func (Decimal) TruncateWithPrec ¶
TruncateWithPrec returns d rounded toward zero at the given precision. It panics if prec is negative.
Example ¶
fmt.Println(MustFromString("-1.239").TruncateWithPrec(2))
Output: -1.23
func (Decimal) Uint64 ¶
Uint64 returns d as a uint64 if it is a non-negative exact integer in range.
Example ¶
v, ok := MustFromString("42.0").Uint64()
fmt.Println(v, ok)
Output: 42 true
func (*Decimal) Unmarshal ¶
Unmarshal implements gogo-protobuf custom type unmarshaling via UnmarshalBinary.
Example ¶
src := MustFromString("1.23")
bz, _ := src.Marshal()
var dst Decimal
_ = dst.Unmarshal(bz)
fmt.Println(dst)
Output: 1.23
func (*Decimal) UnmarshalBinary ¶
UnmarshalBinary implements encoding.BinaryUnmarshaler. It expects the MarshalBinary layout, resets d to zero for empty input, and returns an error when data is shorter than the fixed precision prefix.
Example ¶
src := MustFromString("1.23")
bz, _ := src.MarshalBinary()
var dst Decimal
_ = dst.UnmarshalBinary(bz)
fmt.Println(dst)
Output: 1.23
func (*Decimal) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler. It accepts JSON strings and JSON numbers, treats null as a no-op, and updates d in place.
Example ¶
var d Decimal _ = d.UnmarshalJSON([]byte(`"1.23"`)) fmt.Println(d)
Output: 1.23
func (*Decimal) UnmarshalParam ¶ added in v0.2.0
UnmarshalParam implements gin's BindUnmarshaler by parsing decimal text.
func (*Decimal) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler by parsing decimal text.
Example ¶
var d Decimal
_ = d.UnmarshalText([]byte("1.23"))
fmt.Println(d)
Output: 1.23
func (*Decimal) UnmarshalXML ¶ added in v0.3.0
UnmarshalXML implements xml.Unmarshaler. It parses character data into a Decimal via NewFromString. Empty content leaves d as the uninitialized zero Decimal.
func (*Decimal) UnmarshalXMLAttr ¶ added in v0.3.0
UnmarshalXMLAttr implements xml.UnmarshalerAttr. An empty attribute value leaves d as the uninitialized zero Decimal.
func (*Decimal) UnmarshalYAML ¶
UnmarshalYAML implements yaml unmarshaling by decoding scalar values into Decimal.
Example ¶
var d Decimal
_ = d.UnmarshalYAML(func(target any) error {
p := target.(*any)
*p = "1.23"
return nil
})
fmt.Println(d)
Output: 1.23
type NullDecimal ¶ added in v0.3.0
NullDecimal is a nullable Decimal, mirroring the pattern used by database/sql's NullString. The zero value represents SQL NULL.
Example ¶
package main
import (
"fmt"
"github.com/exc-works/decimal"
)
func main() {
n := decimal.NewNullDecimal(decimal.MustFromString("9.99"))
fmt.Println(n.Valid, n.Decimal)
var empty decimal.NullDecimal
fmt.Println(empty.Valid, empty.String())
}
Output: true 9.99 false null
func NewNullDecimal ¶ added in v0.3.0
func NewNullDecimal(d Decimal) NullDecimal
NewNullDecimal returns a NullDecimal wrapping d with Valid set to true.
func (NullDecimal) MarshalJSON ¶ added in v0.3.0
func (n NullDecimal) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler. An invalid NullDecimal is encoded as JSON null.
Example ¶
package main
import (
"encoding/json"
"fmt"
"github.com/exc-works/decimal"
)
func main() {
valid := decimal.NewNullDecimal(decimal.MustFromString("1.5"))
validBytes, _ := json.Marshal(valid)
var invalid decimal.NullDecimal
invalidBytes, _ := json.Marshal(invalid)
fmt.Println(string(validBytes))
fmt.Println(string(invalidBytes))
}
Output: "1.5" null
func (NullDecimal) MarshalText ¶ added in v0.3.0
func (n NullDecimal) MarshalText() ([]byte, error)
MarshalText implements encoding.TextMarshaler. An invalid NullDecimal is encoded as an empty byte slice.
func (NullDecimal) MarshalXML ¶ added in v0.3.0
func (n NullDecimal) MarshalXML(e *xml.Encoder, start xml.StartElement) error
MarshalXML implements xml.Marshaler. An invalid NullDecimal is encoded as an empty element; otherwise the call is delegated to Decimal.MarshalXML.
func (NullDecimal) MarshalXMLAttr ¶ added in v0.3.0
MarshalXMLAttr implements xml.MarshalerAttr. An invalid NullDecimal returns an empty xml.Attr which encoding/xml omits. Otherwise the call is delegated to Decimal.MarshalXMLAttr.
func (NullDecimal) MarshalYAML ¶ added in v0.3.0
func (n NullDecimal) MarshalYAML() (any, error)
MarshalYAML implements yaml.Marshaler. An invalid NullDecimal is encoded as the YAML nil value.
func (*NullDecimal) Scan ¶ added in v0.3.0
func (n *NullDecimal) Scan(value any) error
Scan implements sql.Scanner. A nil value resets n to an invalid zero state. Any other value is delegated to Decimal.Scan and marks n valid on success.
func (NullDecimal) String ¶ added in v0.3.0
func (n NullDecimal) String() string
String returns "null" when n is not valid, otherwise Decimal.String.
func (*NullDecimal) UnmarshalJSON ¶ added in v0.3.0
func (n *NullDecimal) UnmarshalJSON(data []byte) error
UnmarshalJSON implements json.Unmarshaler. JSON null sets Valid to false; any other value is parsed via Decimal.UnmarshalJSON.
Example ¶
package main
import (
"encoding/json"
"fmt"
"github.com/exc-works/decimal"
)
func main() {
var valid decimal.NullDecimal
_ = json.Unmarshal([]byte(`"2.5"`), &valid)
fmt.Println(valid.Valid, valid.Decimal)
var null decimal.NullDecimal
_ = json.Unmarshal([]byte(`null`), &null)
fmt.Println(null.Valid)
}
Output: true 2.5 false
func (*NullDecimal) UnmarshalParam ¶ added in v0.3.0
func (n *NullDecimal) UnmarshalParam(param string) error
UnmarshalParam implements gin's BindUnmarshaler. An empty string marks the value invalid; any other value is delegated to Decimal.UnmarshalParam.
func (*NullDecimal) UnmarshalText ¶ added in v0.3.0
func (n *NullDecimal) UnmarshalText(text []byte) error
UnmarshalText implements encoding.TextUnmarshaler. Empty text sets Valid to false; any other value is parsed via Decimal.UnmarshalText.
func (*NullDecimal) UnmarshalXML ¶ added in v0.3.0
func (n *NullDecimal) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error
UnmarshalXML implements xml.Unmarshaler. Empty character content sets Valid to false; any other value is delegated to Decimal.UnmarshalXML and marks n valid on success.
func (*NullDecimal) UnmarshalXMLAttr ¶ added in v0.3.0
func (n *NullDecimal) UnmarshalXMLAttr(attr xml.Attr) error
UnmarshalXMLAttr implements xml.UnmarshalerAttr. An empty attribute value sets Valid to false; any other value is delegated to Decimal.UnmarshalXMLAttr and marks n valid on success.
func (*NullDecimal) UnmarshalYAML ¶ added in v0.3.0
func (n *NullDecimal) UnmarshalYAML(unmarshal func(any) error) error
UnmarshalYAML implements yaml unmarshaling. A nil scalar sets Valid to false; other scalars are delegated to Decimal.UnmarshalYAML.
type RoundingMode ¶
type RoundingMode int
const ( // RoundDown rounds towards zero. RoundDown RoundingMode = iota // RoundUp rounds away from zero. RoundUp // RoundCeiling rounds towards positive infinity. RoundCeiling // RoundHalfUp rounds to nearest; ties round up. RoundHalfUp // RoundHalfDown rounds to nearest; ties round down. RoundHalfDown // RoundHalfEven rounds to nearest; ties to even. RoundHalfEven // RoundUnnecessary asserts no rounding is required. RoundUnnecessary )