Documentation ¶
Index ¶
- Constants
- Variables
- func RescalePair(d1 Decimal, d2 Decimal) (Decimal, Decimal)
- func SizeAndScaleFromString(s string) (int32, int32)
- type Decimal
- func New(value int64, exp int32) Decimal
- func NewFromFloat(value float64) Decimal
- func NewFromFloat32(value float32) Decimal
- func NewFromFloatMySQL(value float64) Decimal
- func NewFromInt(value int64) Decimal
- func NewFromMySQL(s []byte) (Decimal, error)
- func NewFromString(s string) (d Decimal, err error)
- func NewFromUint(value uint64) Decimal
- func RequireFromString(value string) Decimal
- func (d Decimal) Abs() Decimal
- func (d Decimal) Add(d2 Decimal) Decimal
- func (cached *Decimal) CachedSize(alloc bool) int64
- func (d Decimal) Ceil() Decimal
- func (d Decimal) Clamp(integral, fractional int32) Decimal
- func (d Decimal) Cmp(d2 Decimal) int
- func (d Decimal) CmpAbs(d2 Decimal) int
- func (d Decimal) Copy() Decimal
- func (d Decimal) Div(d2 Decimal, scaleIncr int32) Decimal
- func (d Decimal) Equal(d2 Decimal) bool
- func (d Decimal) Exponent() int32
- func (d Decimal) Float64() (f float64, ok bool)
- func (d Decimal) Floor() Decimal
- func (d Decimal) FormatMySQL(frac int32) []byte
- func (d *Decimal) Hash(hasher *vthash.Hasher)
- func (d Decimal) Int64() (int64, bool)
- func (d Decimal) IsInitialized() bool
- func (d Decimal) IsZero() bool
- func (d Decimal) Mul(d2 Decimal) Decimal
- func (d Decimal) Neg() Decimal
- func (d Decimal) NegInPlace() Decimal
- func (d Decimal) QuoRem(d2 Decimal, precision int32) (Decimal, Decimal)
- func (d Decimal) Round(places int32) Decimal
- func (d Decimal) Sign() int
- func (d Decimal) String() string
- func (d Decimal) StringFixed(places int32) string
- func (d Decimal) StringMySQL() string
- func (d Decimal) Sub(d2 Decimal) Decimal
- func (d Decimal) Truncate(precision int32) Decimal
- func (d Decimal) Uint64() (uint64, bool)
- func (d Decimal) WeightString(dst []byte, length, precision int32) []byte
Constants ¶
const ExponentLimit = 1024
const MyMaxBigDigits = 9
MyMaxBigDigits is the largest amount of "big digits" that MySQL supports See: myBigDigits
const MyMaxPrecision = 65
MyMaxPrecision is the largest precision on a decimal that MySQL supports
const MyMaxScale = 30
MyMaxScale is the largest scale on a decimal that MySQL supports
Variables ¶
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 ¶
RescalePair rescales two decimals to common exponential value (minimal exp of both decimals)
func SizeAndScaleFromString ¶
SizeAndScaleFromString gets the size and scale for the decimal value without needing to parse it.
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 NewFromFloat ¶
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 NewFromFloatMySQL ¶
func NewFromInt ¶
NewFromInt converts a int64 to Decimal.
Example:
NewFromInt(123).String() // output: "123" NewFromInt(-10).String() // output: "-10"
func NewFromMySQL ¶
func NewFromString ¶
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 RequireFromString ¶
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) CachedSize ¶
func (Decimal) Cmp ¶
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) Copy ¶
Copy returns a copy of decimal with the same value and exponent, but a different pointer to value.
func (Decimal) Float64 ¶
Float64 returns the nearest float64 value for d and a bool indicating whether f represents d exactly.
func (Decimal) FormatMySQL ¶
func (Decimal) IsInitialized ¶
func (Decimal) NegInPlace ¶
func (Decimal) QuoRem ¶
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 ¶
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) 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 ¶
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"