decimal

package
v0.19.4 Latest Latest
Warning

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

Go to latest
Published: May 8, 2024 License: Apache-2.0, MIT Imports: 10 Imported by: 2

Documentation

Index

Constants

View Source
const ExponentLimit = 1024
View Source
const MyMaxBigDigits = 9

MyMaxBigDigits is the largest amount of "big digits" that MySQL supports See: myBigDigits

View Source
const MyMaxPrecision = 65

MyMaxPrecision is the largest precision on a decimal that MySQL supports

View Source
const MyMaxScale = 30

MyMaxScale is the largest scale on a decimal that MySQL supports

Variables

View Source
var Zero = New(0, 0)

Zero constant, to make computations faster. Zero should never be compared with == or != directly, please use decimal.Equal or decimal.Cmp instead.

Functions

func RescalePair

func RescalePair(d1 Decimal, d2 Decimal) (Decimal, Decimal)

RescalePair rescales two decimals to common exponential value (minimal exp of both decimals)

Types

type Decimal

type Decimal struct {
	// contains filtered or unexported fields
}

Decimal represents a fixed-point decimal. It is immutable. number = value * 10 ^ exp

func New

func New(value int64, exp int32) Decimal

New returns a new fixed-point decimal, value * 10 ^ exp.

func NewFromFloat

func NewFromFloat(value float64) Decimal

NewFromFloat converts a float64 to Decimal.

The converted number will contain the number of significant digits that can be represented in a float with reliable roundtrip. This is typically 15 digits, but may be more in some cases. See https://www.exploringbinary.com/decimal-precision-of-binary-floating-point-numbers/ for more information.

For slightly faster conversion, use NewFromFloatWithExponent where you can specify the precision in absolute terms.

NOTE: this will panic on NaN, +/-inf

func NewFromFloat32

func NewFromFloat32(value float32) Decimal

func NewFromFloatMySQL

func NewFromFloatMySQL(value float64) Decimal

func NewFromInt

func NewFromInt(value int64) Decimal

NewFromInt converts a int64 to Decimal.

Example:

NewFromInt(123).String() // output: "123"
NewFromInt(-10).String() // output: "-10"

func NewFromMySQL

func NewFromMySQL(s []byte) (Decimal, error)

func NewFromString

func NewFromString(s string) (d Decimal, err error)

NewFromString returns a new Decimal from a string representation. Trailing zeroes are not trimmed. In case of an error, we still return the parsed value up to that point.

Example:

d, err := NewFromString("-123.45")
d2, err := NewFromString(".0001")
d3, err := NewFromString("1.47000")

func NewFromUint

func NewFromUint(value uint64) Decimal

func RequireFromString

func RequireFromString(value string) Decimal

RequireFromString returns a new Decimal from a string representation or panics if NewFromString would have returned an error.

Example:

d := RequireFromString("-123.45")
d2 := RequireFromString(".0001")

func (Decimal) Abs

func (d Decimal) Abs() Decimal

abs returns the absolute value of the decimal.

func (Decimal) Add

func (d Decimal) Add(d2 Decimal) Decimal

Add returns d + d2.

func (*Decimal) CachedSize

func (cached *Decimal) CachedSize(alloc bool) int64

func (Decimal) Ceil

func (d Decimal) Ceil() Decimal

func (Decimal) Clamp

func (d Decimal) Clamp(integral, fractional int32) Decimal

func (Decimal) Cmp

func (d Decimal) Cmp(d2 Decimal) int

Cmp compares the numbers represented by d and d2 and returns:

-1 if d <  d2
 0 if d == d2
+1 if d >  d2

func (Decimal) CmpAbs

func (d Decimal) CmpAbs(d2 Decimal) int

func (Decimal) Copy

func (d Decimal) Copy() Decimal

Copy returns a copy of decimal with the same value and exponent, but a different pointer to value.

func (Decimal) Div

func (d Decimal) Div(d2 Decimal, scaleIncr int32) Decimal

func (Decimal) Equal

func (d Decimal) Equal(d2 Decimal) bool

Equal returns whether the numbers represented by d and d2 are equal.

func (Decimal) Exponent

func (d Decimal) Exponent() int32

Exponent returns the exponent, or scale component of the decimal.

func (Decimal) Float64

func (d Decimal) Float64() (f float64, ok bool)

Float64 returns the nearest float64 value for d and a bool indicating whether f represents d exactly.

func (Decimal) Floor

func (d Decimal) Floor() Decimal

func (Decimal) FormatMySQL

func (d Decimal) FormatMySQL(frac int32) []byte

func (*Decimal) Hash

func (d *Decimal) Hash(hasher *vthash.Hasher)

func (Decimal) Int64

func (d Decimal) Int64() (int64, bool)

func (Decimal) IsInitialized added in v0.18.0

func (d Decimal) IsInitialized() bool

func (Decimal) IsZero

func (d Decimal) IsZero() bool

IsZero return

true if d == 0
false if d > 0
false if d < 0

func (Decimal) Mul

func (d Decimal) Mul(d2 Decimal) Decimal

func (Decimal) Neg

func (d Decimal) Neg() Decimal

Neg returns -d.

func (Decimal) NegInPlace

func (d Decimal) NegInPlace() Decimal

func (Decimal) QuoRem

func (d Decimal) QuoRem(d2 Decimal, precision int32) (Decimal, Decimal)

QuoRem does division with remainder d.QuoRem(d2,precision) returns quotient q and remainder r such that

d = d2 * q + r, q an integer multiple of 10^(-precision)
0 <= r < abs(d2) * 10 ^(-precision) if d>=0
0 >= r > -abs(d2) * 10 ^(-precision) if d<0

Note that precision<0 is allowed as input.

func (Decimal) Round

func (d Decimal) Round(places int32) Decimal

Round rounds the decimal to places decimal places. If places < 0, it will round the integer part to the nearest 10^(-places).

Example:

NewFromFloat(5.45).Round(1).String() // output: "5.5"
NewFromFloat(545).Round(-1).String() // output: "550"

func (Decimal) Sign

func (d Decimal) Sign() int

Sign returns:

-1 if d <  0
 0 if d == 0
+1 if d >  0

func (Decimal) String

func (d Decimal) String() string

String returns the string representation of the decimal with the fixed point.

Example:

d := New(-12345, -3)
println(d.String())

Output:

-12.345

func (Decimal) StringFixed

func (d Decimal) StringFixed(places int32) string

StringFixed returns a rounded fixed-point string with places digits after the decimal point.

Example:

NewFromFloat(0).StringFixed(2) // output: "0.00"
NewFromFloat(0).StringFixed(0) // output: "0"
NewFromFloat(5.45).StringFixed(0) // output: "5"
NewFromFloat(5.45).StringFixed(1) // output: "5.5"
NewFromFloat(5.45).StringFixed(2) // output: "5.45"
NewFromFloat(5.45).StringFixed(3) // output: "5.450"
NewFromFloat(545).StringFixed(-1) // output: "550"

func (Decimal) StringMySQL

func (d Decimal) StringMySQL() string

func (Decimal) Sub

func (d Decimal) Sub(d2 Decimal) Decimal

func (Decimal) Truncate

func (d Decimal) Truncate(precision int32) Decimal

func (Decimal) Uint64

func (d Decimal) Uint64() (uint64, bool)

func (Decimal) WeightString added in v0.18.0

func (d Decimal) WeightString(dst []byte, length, precision int32) []byte

Jump to

Keyboard shortcuts

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