numeric

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2026 License: MIT Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package numeric provides Decimal, an arbitrary-precision decimal type for prices, balances, and other exact amounts where float64 rounding is unacceptable.

Construct values with Parse, MustParse, FromInt, or New:

price, err := numeric.Parse("19.99")
total := price.Mul(numeric.FromInt(3)) // 59.97

Decimal scans from and writes to SQL NUMERIC and DECIMAL columns, decodes from form fields and JSON, and encodes to JSON as a string so clients retain every digit.

Index

Constants

This section is empty.

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 decimal number with a fixed scale, which is the number of digits after the decimal point (for example, 19.990 has a scale of 3). The zero value represents 0.

Decimal values are immutable, so arithmetic methods return a new Decimal and values are safe to share across goroutines. Compare values with Decimal.Cmp or Decimal.Equal rather than ==, because equal numbers such as 1.5 and 1.50 may have different scales.

func FromInt

func FromInt(n int64) Decimal

FromInt returns n as a Decimal with a scale of 0.

func MustParse

func MustParse(s string) Decimal

MustParse parses s like Parse and panics if s is not a valid decimal number. Use it for constant values written directly in source code.

func New

func New(unscaled int64, scale int32) Decimal

New returns unscaled divided by 10 to the power of scale. For example, New(1999, 2) returns 19.99, which is convenient for amounts stored in cents. A negative scale is treated as 0.

func Parse

func Parse(s string) (Decimal, error)

Parse parses a decimal string such as "19.99", "-0.5", or "1.5e3". It preserves trailing fractional zeros, so "19.90" has a scale of 2. Parse returns an error if s is not a valid decimal, exceeds 1000 significant digits, or has an exponent or effective scale outside -1000 to 1000.

func (Decimal) Abs

func (d Decimal) Abs() Decimal

Abs returns the absolute value of d with the same scale.

func (Decimal) Add

func (d Decimal) Add(other Decimal) Decimal

Add returns the exact sum d + other, using the larger of their two scales.

func (Decimal) Cmp

func (d Decimal) Cmp(other Decimal) int

Cmp compares d and other numerically, ignoring scale, and returns -1 if d is less than other, 0 if they are equal, and 1 if d is greater. For example, 1.5 and 1.50 compare equal.

func (Decimal) Div

func (d Decimal) Div(other Decimal, places int) Decimal

Div returns d divided by other, rounded half away from zero to places digits after the decimal point. Div panics if other is zero, so check any divisor coming from untrusted input before calling it.

func (Decimal) Equal

func (d Decimal) Equal(other Decimal) bool

Equal reports whether d and other represent the same numeric value, regardless of scale.

func (Decimal) Float64

func (d Decimal) Float64() float64

Float64 returns the nearest float64 representation of d.

func (Decimal) GoString

func (d Decimal) GoString() string

GoString formats d as a Go expression such as numeric.MustParse("19.90") so %#v output, test failures, and debug dumps display the exact value.

func (Decimal) IsZero

func (d Decimal) IsZero() bool

IsZero reports whether d equals zero, regardless of its scale.

func (Decimal) MarshalJSON

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

MarshalJSON encodes d as a JSON string such as "19.99" so clients that parse JSON numbers as floating-point values preserve every digit.

func (Decimal) MarshalText

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

MarshalText encodes d as UTF-8 text matching Decimal.String.

func (Decimal) Mul

func (d Decimal) Mul(other Decimal) Decimal

Mul returns the exact product d * other, with a scale equal to the sum of their scales.

func (Decimal) Neg

func (d Decimal) Neg() Decimal

Neg returns the negated value -d with the same scale.

func (Decimal) Round

func (d Decimal) Round(places int) Decimal

Round returns d rounded half away from zero to places digits after the decimal point. If d has fewer fractional digits than places, Round pads it with trailing zeros. For example, Round(2) turns 1.005 into 1.01 and 1.5 into 1.50.

func (Decimal) Scale

func (d Decimal) Scale() int

Scale returns the number of digits after the decimal point in d.

func (*Decimal) Scan

func (d *Decimal) Scan(src any) error

Scan implements database/sql.Scanner, reading text or bytes from NUMERIC and DECIMAL columns as well as integer or floating-point values returned by SQLite. Floating-point values keep the shortest decimal representation that parses back to the same float. Scan returns an error for NULL values or unsupported types; scan nullable columns into a *Decimal instead.

func (Decimal) Sign

func (d Decimal) Sign() int

Sign returns -1 if d is negative, 0 if d is zero, and 1 if d is positive.

func (Decimal) String

func (d Decimal) String() string

String formats d with all of its fractional digits (such as "19.90") and without scientific notation.

func (Decimal) Sub

func (d Decimal) Sub(other Decimal) Decimal

Sub returns the exact difference d - other, using the larger of their two scales.

func (*Decimal) UnmarshalJSON

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

UnmarshalJSON decodes a JSON string or number such as "19.99" or 19.99 into d. A JSON null leaves d unchanged. UnmarshalJSON returns an error if data is not a valid decimal.

func (*Decimal) UnmarshalText

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

UnmarshalText parses a decimal string using Parse, such as when decoding a form field. It returns an error if text is not a valid decimal.

func (Decimal) Value

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

Value implements driver.Valuer by returning d formatted as text, which databases convert to their exact decimal column types.

Jump to

Keyboard shortcuts

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