decimal

package
v0.1.133 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package decimal is the Go type of a schema.lidza decimal(p, s) field, a Postgres numeric(p, s) column: an exact decimal number kept as its text, "12.50", never as a float. JSON carries it as a string, so no client parses it into a binary float on the way; pgx reads and writes it as numeric, and sqlc maps numeric columns to it.

Arithmetic goes through math/big: Rat gives the exact value, FromRat rounds a result back to a scale.

total := new(big.Rat).Mul(price.Rat(), big.NewRat(int64(qty), 1))
order.Total = decimal.FromRat(total, 2)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Decimal

type Decimal string

Decimal is an exact decimal number in plain notation: an optional minus sign, digits, and optionally a point and more digits. The zero value "" is 0. A literal converts directly (Price: "12.50"); text from outside goes through Parse.

func FromRat

func FromRat(r *big.Rat, scale int) Decimal

FromRat renders r with scale digits after the point, rounding half away from zero as Postgres does.

func MustParse

func MustParse(s string) Decimal

MustParse is Parse for literals known to be valid; it panics otherwise.

func Parse

func Parse(s string) (Decimal, error)

Parse reads a decimal number: "12.50", "-3", "+0.5", or with an exponent, "1.25e3". The result is in plain notation with the sign normalized and redundant leading zeros removed; trailing zeros stay, since they carry the scale ("12.50").

func (Decimal) Cmp

func (d Decimal) Cmp(o Decimal) int

Cmp compares d and o exactly: -1 when d < o, 0 when equal, +1 when d > o. A value that is not a number compares as 0.

func (Decimal) Fits

func (d Decimal) Fits(precision, scale int) bool

Fits reports whether d is a number a numeric(precision, scale) column holds without rounding: at most precision-scale digits before the point and scale after it (trailing zeros aside).

func (Decimal) MarshalJSON

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

MarshalJSON writes the number as a JSON string, "12.50".

func (Decimal) MarshalText

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

MarshalText and UnmarshalText let a Decimal be a map key or a query parameter.

func (Decimal) NumericValue

func (d Decimal) NumericValue() (pgtype.Numeric, error)

NumericValue writes d to a numeric column through pgx.

func (Decimal) Rat

func (d Decimal) Rat() *big.Rat

Rat returns the exact value, or nil when d is not a number.

func (*Decimal) Scan

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

Scan reads a numeric column through database/sql.

func (*Decimal) ScanNumeric

func (d *Decimal) ScanNumeric(n pgtype.Numeric) error

ScanNumeric reads a numeric column through pgx.

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 number; the zero value is "0".

func (*Decimal) UnmarshalJSON

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

UnmarshalJSON reads a JSON string ("12.50") or a JSON number (12.5, kept exactly as written).

func (*Decimal) UnmarshalText

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

func (Decimal) Valid

func (d Decimal) Valid() bool

Valid reports whether d is a decimal number (the zero value is).

func (Decimal) Value

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

Value writes d through database/sql, as text.

Jump to

Keyboard shortcuts

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