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 ¶
- type Decimal
- func (d Decimal) Abs() Decimal
- func (d Decimal) Add(other Decimal) Decimal
- func (d Decimal) Cmp(other Decimal) int
- func (d Decimal) Div(other Decimal, places int) Decimal
- func (d Decimal) Equal(other Decimal) bool
- func (d Decimal) Float64() float64
- func (d Decimal) GoString() string
- func (d Decimal) IsZero() bool
- func (d Decimal) MarshalJSON() ([]byte, error)
- func (d Decimal) MarshalText() ([]byte, error)
- func (d Decimal) Mul(other Decimal) Decimal
- func (d Decimal) Neg() Decimal
- func (d Decimal) Round(places int) Decimal
- func (d Decimal) Scale() int
- func (d *Decimal) Scan(src any) error
- func (d Decimal) Sign() int
- func (d Decimal) String() string
- func (d Decimal) Sub(other Decimal) Decimal
- func (d *Decimal) UnmarshalJSON(data []byte) error
- func (d *Decimal) UnmarshalText(text []byte) error
- func (d Decimal) Value() (driver.Value, error)
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 MustParse ¶
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 ¶
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 ¶
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) Cmp ¶
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 ¶
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 ¶
Equal reports whether d and other represent the same numeric value, regardless of scale.
func (Decimal) GoString ¶
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) MarshalJSON ¶
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 ¶
MarshalText encodes d as UTF-8 text matching Decimal.String.
func (Decimal) Mul ¶
Mul returns the exact product d * other, with a scale equal to the sum of their scales.
func (Decimal) Round ¶
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) Scan ¶
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) String ¶
String formats d with all of its fractional digits (such as "19.90") and without scientific notation.
func (Decimal) Sub ¶
Sub returns the exact difference d - other, using the larger of their two scales.
func (*Decimal) UnmarshalJSON ¶
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 ¶
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.