Documentation
¶
Index ¶
- Constants
- func Compare[A, B Number](a A, b B) int
- func Equal[A, B Number](a A, b B) bool
- type Decimal
- func Absolute[N Number](n N) Decimal
- func Add[A, B Number](a A, b B) Decimal
- func Divide[A, B Number](a A, b B) Decimal
- func Multiply[A, B Number](a A, b B) Decimal
- func Negate[N Number](n N) Decimal
- func New[N Number](value N) Decimal
- func NewFromString(s string) (Decimal, error)
- func NewFromStringFuzzy(s string) (Decimal, error)
- func Subtract[A, B Number](a A, b B) Decimal
- func Zero() Decimal
- func (d Decimal) Float64() float64
- func (d Decimal) Format(state fmt.State, verb rune)
- func (d Decimal) IsZero() bool
- func (d Decimal) MarshalCBOR() ([]byte, error)
- func (d Decimal) MarshalJSON() ([]byte, error)
- func (d Decimal) MarshalText() ([]byte, error)
- func (d Decimal) Round(digits uint8) Decimal
- func (d *Decimal) Scan(value any) (err error)
- func (d Decimal) String() string
- func (d Decimal) ToDigits(digits uint8) Decimal
- func (d Decimal) Truncate() Decimal
- func (d *Decimal) UnmarshalCBOR(data []byte) error
- func (d *Decimal) UnmarshalJSON(data []byte) error
- func (d *Decimal) UnmarshalText(data []byte) error
- func (d Decimal) Value() (driver.Value, error)
- type Number
Constants ¶
const ( CBOR_MAJOR = 0b111_00000 CBOR_ADDITIONAL = 0b000_11111 CBOR_INTPOS = 0b000_00000 CBOR_INTNEG = 0b001_00000 CBOR_BYTESTRING = 0b010_00000 CBOR_ARRAY_LEN2 = 0b100_00010 CBOR_TAG = 0b110_00000 CBOR_TAG_BIGNUMPOS = 0b110_00010 CBOR_TAG_BIGNUMNEG = 0b110_00011 CBOR_TAG_DECIMALFRAC = 0b110_00100 CBOR_TYPE7 = 0b111_00000 CBOR_FLOAT16 = 0b111_11001 CBOR_FLOAT32 = 0b111_11010 CBOR_FLOAT64 = 0b111_11011 )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Decimal ¶
type Decimal struct {
Negative bool
Digits uint8 // Number of digits in Fraction, must be less or equal to 19
Integer uint64
Fraction uint64
}
Decimal represents high precision decimal numbers. It can store numbers with up to 19 digits behind the decimal point. The integer component is constrained to the range of a 64-bit unsigned integer in both positive and negative values. When manually constructed or modified, `Digits` must represent the exact number of digits in `Fraction`. Values with more than 19 fractional digits as well as negative zero are unsupported and will trigger undefined behavior.
func Add ¶ added in v0.4.0
Add adds two decimals together. Its overflow behavior matches that of integers in Go.
func Divide ¶ added in v0.4.0
Divide divides two decimal values. The result is computed with maximum precision (19 fractional digits). Its overflow and divide-by-zero behavior match that of integers in Go.
func Multiply ¶ added in v0.4.0
Multiply multiplies two decimal values. Its overflow behavior matches that of integers in Go.
func New ¶
New converts any of the builtin numeric types in Go to a decimal value. It represents integer values exactly. 32-bit floating point numbers are converted with 10 digits behind the decimal point. 64-bit floating point numbers are converted with 18 digits behind the decimal point. Floating point numbers exceeding the unsigned 64-bit integer range inherit Go's float-to-uint64 overflow logic. Positive and negative infinity and NaN cannot be represented and are instead converted to zero.
func NewFromString ¶
NewFromString parses a decimal value from a string. The string must contain just the number with no additional characters around it. It will parse at most 19 digits after the decimal point. The integer component must fit into an unsigned 64-bit integer.
func NewFromStringFuzzy ¶
NewFromStringFuzzy parses the first decimal value it can find from a string. Leading or trailing characters are ignored. The first digit, minus or dot marks the beginning of a number. It will parse at most 19 digits after the decimal point. The integer component must fit into an unsigned 64-bit integer.
func Subtract ¶ added in v0.4.0
Subtract subtracts one decimal value from another. Its overflow behavior matches that of integers in Go.
func (Decimal) Float64 ¶
Float64 converts a decimal value to floating point. Precision loss is minimized but not all values can be represented exactly.
func (Decimal) Format ¶ added in v0.4.0
Format implements fmt.Formatter. It supports "%f" and "%v" but not the other formats specified for floating point values such as "%e".
func (Decimal) MarshalCBOR ¶
MarshalCBOR implements the cbor.Marshaler interface. It encodes the decimal number according to RFC 8949 Section 3.4.4 for Decimal Fractions. The format is a CBOR tag 4 containing a two-element array: [exponent, mantissa].
func (Decimal) MarshalJSON ¶
MarshalJSON encodes a decimal value as a JSON number.
func (Decimal) MarshalText ¶ added in v0.2.0
MarshalText implements encoding.TextMarshaler.
func (Decimal) Round ¶ added in v0.4.0
Round rounds a decimal value to the specified number of digits after the decimal point. It rounds to nearest, ties away from zero. The number of digits is limited to 19.
func (*Decimal) Scan ¶
Scan converts SQL data into a decimal value. It handles textual representation as well as floating point and integer values. Decoding follows the standards set by `New` and `NewFromString` respectively.
func (Decimal) ToDigits ¶
ToDigits converts a decimal value to the specified number of digits after the decimal point. The number of digits is limited to 19. Digits beyond the defined number are truncated, no rounding is performed.
func (*Decimal) UnmarshalCBOR ¶
UnmarshalCBOR implements the cbor.Unmarshaler interface. It supports decoding RFC 8949 Decimal Fractions, standard floats, and integers.
func (*Decimal) UnmarshalJSON ¶
UnmarshalJSON decodes a JSON number or string into a decimal value. Strings must be a plain number and may not contain any escaped or non-numeric characters. `null` is decoded as zero to ensure missing values do not stop decoding entirely.
func (*Decimal) UnmarshalText ¶ added in v0.2.0
UnmarshalText implements encoding.TextUnmarshaler.