decimal

package
v3.123.0 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2025 License: Apache-2.0 Imports: 8 Imported by: 0

README

Decimal package

Package decimal provides tools for working with YDB's decimal types.

Decimal types are stored as int128 values inside YDB and represented as 16-byte arrays in ydb package and as *math/big.Int in ydb/decimal package.

Note that returned big.Int values are scaled. That is, math operations must be prepared keeping in mind scaling factor:

import (
	"ydb/decimal"
	"math/big"
)

var scaleFactor = big.NewInt(10000000000) // Default scale is 9.

func main() {
	x := decimal.FromInt128([16]byte{...})
	x.Add(x, big.NewInt(42)) // Incorrect.
	x.Add(x, scale(42))      // Correct.
}

func scale(n int64) *big.Int {
	x := big.NewInt(n)
	return x.Mul(x, scaleFactor)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Append

func Append(p []byte, x *big.Int) []byte

func BigIntToByte

func BigIntToByte(x *big.Int, precision uint32) (p [16]byte)

BigIntToByte returns the 16-byte array representation of x.

If x value does not fit in 16 bytes with given precision, it returns 16-byte representation of infinity or negative infinity value accordingly to x's sign.

func Err

func Err() *big.Int

Err returns "error" value.

func Format

func Format(x *big.Int, precision, scale uint32, trimTrailingZeros bool) string

Format returns the string representation of x with the given precision and scale.

func FromBytes

func FromBytes(bts []byte, precision uint32) *big.Int

FromBytes converts bytes representation of decimal to big integer. Most callers should use FromInt128().

If given bytes contains value that is greater than given precision it returns infinity or negative infinity value accordingly the bytes sign.

func FromInt128

func FromInt128(p [16]byte, precision uint32) *big.Int

FromInt128 returns big integer from given array. That is, it interprets 16-byte array as 128-bit integer.

func Inf

func Inf() *big.Int

Inf returns infinity value.

func IsErr

func IsErr(x *big.Int) bool

IsErr reports whether x is an "error" value.

func IsInf

func IsInf(x *big.Int) bool

IsInf reports whether x is an infinity.

func IsNaN

func IsNaN(x *big.Int) bool

IsNaN reports whether x is a "not-a-number" value.

func NaN

func NaN() *big.Int

NaN returns "not-a-number" value.

func Parse

func Parse(s string, precision, scale uint32) (*big.Int, error)

Parse interprets a string s with the given precision and scale and returns the corresponding big integer.

func ParseDecimal

func ParseDecimal(s string) (_ *big.Int, exp uint32, _ error)

ParseDecimal parses a decimal string into a big.Int and exponent. Returns (n, e) such that n * 10^(-e) equals the original number.

Types

type Decimal

type Decimal struct {
	Bytes     [16]byte
	Precision uint32
	Scale     uint32
}

func ToDecimal

func ToDecimal(v Interface) *Decimal

func (Decimal) BigInt

func (d Decimal) BigInt() *big.Int

func (*Decimal) Decimal

func (d *Decimal) Decimal() (bytes [16]byte, precision uint32, scale uint32)

func (Decimal) Format

func (d Decimal) Format(trimTrailingZeros bool) string

func (*Decimal) Scan

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

func (Decimal) String

func (d Decimal) String() string

type Interface

type Interface interface {
	Decimal() (bytes [16]byte, precision uint32, scale uint32)
}

type ParseError

type ParseError struct {
	Err   error
	Input string
}

func (*ParseError) Error

func (p *ParseError) Error() string

func (*ParseError) Unwrap

func (p *ParseError) Unwrap() error

Jump to

Keyboard shortcuts

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