Documentation ¶
Overview ¶
Package decimal implements an arbitrary precision fixed-point decimal.
To use as part of a struct:
type Struct struct { Number Decimal }
The zero-value of a Decimal is 0, as you would expect.
The best way to create a new Decimal is to use decimal.NewFromString, ex:
n, err := decimal.NewFromString("-123.4567") n.String() // output: "-123.4567"
NOTE: This can "only" represent numbers with a maximum of 2^31 digits after the decimal point.
Index ¶
- Variables
- func SetComparePrecision(precision float64)
- func SetStringPrecision(value int32)
- type Decimal
- func F(value float64) Decimal
- func Max(first Decimal, rest ...Decimal) Decimal
- func Min(first Decimal, rest ...Decimal) Decimal
- func N(value string) Decimal
- func New(value int64, exp int32) Decimal
- func NewFromFloat(value float64) Decimal
- func NewFromFloatWithExponent(value float64, exp int32) Decimal
- func NewFromString(value string) (Decimal, error)
- func (d Decimal) Abs() Decimal
- func (d Decimal) Add(d2 Decimal) Decimal
- func (d Decimal) Ceil() Decimal
- func (d Decimal) Cmp(d2 Decimal) int
- func (d Decimal) Copy() Decimal
- func (d Decimal) Div(d2 Decimal) Decimal
- func (d Decimal) Eq(d2 Decimal) bool
- func (d Decimal) Equals(d2 Decimal) bool
- func (d Decimal) Exponent() int32
- func (d Decimal) Float64() (f float64, exact bool)
- func (d Decimal) Float64f() float64
- func (d Decimal) Floor() Decimal
- func (d Decimal) Ge(d2 Decimal) bool
- func (d Decimal) Gt(d2 Decimal) bool
- func (d Decimal) IntPart() int64
- func (d Decimal) Le(d2 Decimal) bool
- func (d Decimal) Lt(d2 Decimal) bool
- func (d Decimal) MarshalJSON() ([]byte, error)
- func (d Decimal) MarshalText() (text []byte, err error)
- func (d Decimal) Mul(d2 Decimal) Decimal
- func (d Decimal) Ne(d2 Decimal) bool
- func (d Decimal) Neg() Decimal
- func (d Decimal) P() *Decimal
- func (d Decimal) Rat() *big.Rat
- func (d Decimal) Round(places int32) Decimal
- func (d *Decimal) Scan(value interface{}) error
- func (d Decimal) String() string
- func (d Decimal) StringFixed(places int32) string
- func (d Decimal) StringScaled(exp int32) string
- func (d Decimal) Sub(d2 Decimal) Decimal
- func (d Decimal) Truncate(precision int32) Decimal
- func (d *Decimal) UnmarshalJSON(decimalBytes []byte) error
- func (d *Decimal) UnmarshalText(text []byte) error
- func (d Decimal) Value() (driver.Value, error)
- type Decimals
- func (decimals Decimals) Copy() Decimals
- func (decimals Decimals) Equal(decimals2 Decimals, ordered bool, precise bool) bool
- func (decimals Decimals) Len() int
- func (decimals Decimals) Less(i, j int) bool
- func (decimals Decimals) RemoveDuplicates() Decimals
- func (decimals Decimals) Sort()
- func (decimals Decimals) Swap(i, j int)
Constants ¶
This section is empty.
Variables ¶
var DivisionPrecision = 16
DivisionPrecision is the number of decimal places in the result when it doesn't divide exactly.
Example:
d1 := decimal.NewFromFloat(2).Div(decimal.NewFromFloat(3) d1.String() // output: "0.6666666666666667" d2 := decimal.NewFromFloat(2).Div(decimal.NewFromFloat(30000) d2.String() // output: "0.0000666666666667" d3 := decimal.NewFromFloat(20000).Div(decimal.NewFromFloat(3) d3.String() // output: "6666.6666666666666667" decimal.DivisionPrecision = 3 d4 := decimal.NewFromFloat(2).Div(decimal.NewFromFloat(3) d4.String() // output: "0.667"
var Zero = New(0, 1)
Zero constant, to make computations faster.
Functions ¶
func SetComparePrecision ¶
func SetComparePrecision(precision float64)
SetComparePrecision sets the compare precision
func SetStringPrecision ¶
func SetStringPrecision(value int32)
SetStringPrecision sets the precision for string output in Marshaler interfaces
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 Max ¶
Returns the largest Decimal that was passed in the arguments.
To call this function with an array, you must do:
Max(arr[0], arr[1:]...)
This makes it harder to accidentally call Max with 0 arguments.
func Min ¶
Returns the smallest Decimal that was passed in the arguments.
To call this function with an array, you must do:
Min(arr[0], arr[1:]...)
This makes it harder to accidentally call Min with 0 arguments.
func NewFromFloat ¶
NewFromFloat converts a float64 to Decimal.
Example:
NewFromFloat(123.45678901234567).String() // output: "123.4567890123456" NewFromFloat(.00000000000000001).String() // output: "0.00000000000000001"
NOTE: this will panic on NaN, +/-inf
func NewFromFloatWithExponent ¶
NewFromFloatWithExponent converts a float64 to Decimal, with an arbitrary number of fractional digits.
Example:
NewFromFloatWithExponent(123.456, -2).String() // output: "123.46"
func NewFromString ¶
NewFromString returns a new Decimal from a string representation.
Example:
d, err := NewFromString("-123.45") d2, err := NewFromString(".0001")
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) Div ¶
Div returns d / d2. If it doesn't divide exactly, the result will have DivisionPrecision digits after the decimal point.
func (Decimal) Float64 ¶
Float64 returns the nearest float64 value for d and a bool indicating whether f represents d exactly. For more details, see the documentation for big.Rat.Float64
func (Decimal) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
func (Decimal) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface for XML serialization.
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"
func (Decimal) StringScaled ¶
NOTE: buggy, unintuitive, and DEPRECATED! Use StringFixed instead. StringScaled first scales the decimal then calls .String() on it.
func (Decimal) Truncate ¶
Truncate truncates off digits from the number, without rounding.
NOTE: precision is the last digit that will not be truncated (must be >= 0).
Example:
decimal.NewFromString("123.456").Truncate(2).String() // "123.45"
func (*Decimal) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface.
func (*Decimal) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface for XML deserialization.
type Decimals ¶
type Decimals []Decimal
func (Decimals) Equal ¶
Equal compares two slices, decimals and decimals2, of Decimal if ordered is true, comparing order also, otherwise order may differ if precise is true, compare according comparePrecision if slices are equal according to parameter conditions, it returns true, otherwise returns false
func (Decimals) RemoveDuplicates ¶
RemoveDuplicates returns a slice decimals which don't contain duplicate elements from decimals slice