Documentation
¶
Index ¶
- Constants
- type Decimal
- func (d Decimal) Add(d2 Decimal) Decimal
- func (d Decimal) DivideUint64(u uint64) Decimal
- func (d Decimal) Equal(d2 Decimal) bool
- func (d Decimal) Float64() float64
- 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) MultiplyUint64(u uint64) 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 ¶
This section is empty.
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 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 (Decimal) Add ¶
Add adds two decimals together. Its overflow behavior matches that of integers in Go.
func (Decimal) DivideUint64 ¶
DivideUint64 divides a decimal value by an unsigned integer. Its overflow and divide-by-zero behavior match that of integers in Go.
func (Decimal) Equal ¶
Equal checks if 2 decimal values are equal by value. Values with different numbers of decimal digits are considered equal when their exact representations with the least possible number of digits behind the decimal point are equal.
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) 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) MultiplyUint64 ¶
MultiplyUint64 multiplies a decimal value by an unsigned integer. Its overflow behavior matches that of integers in Go.
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.