Documentation ¶
Overview ¶
Package 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
- type Decimal
- func (d Decimal) Abs() Decimal
- func (d Decimal) Add(d2 Decimal) Decimal
- func (d Decimal) Cmp(d2 Decimal) int
- func (d Decimal) Div(d2 Decimal) Decimal
- func (d Decimal) Equals(d2 Decimal) bool
- func (d Decimal) Exponent() int32
- func (d Decimal) Float64() (f float64, exact bool)
- func (d Decimal) IntPart() int64
- func (d Decimal) MarshalJSON() ([]byte, error)
- func (d Decimal) MarshalText() (text []byte, err error)
- func (d Decimal) Mul(d2 Decimal) Decimal
- func (d Decimal) Rat() *big.Rat
- func (d *Decimal) Scan(value interface{}) error
- func (d Decimal) String() 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)
Constants ¶
This section is empty.
Variables ¶
var DivisionPrecision = 16
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)
constant, to make computations faster
Functions ¶
This section is empty.
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 ¶
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 ¶
Same as NewFromFloat, except you can choose the number of fractional digits
Example:
NewFromFloatWithExponent(123.456, -2).String() // output: "123.46"
func NewFromString ¶
Returns a new Decimal from a string representation
Example:
d, err := NewFromString("-123.45") d2, err := NewFromString(".0001")
func (Decimal) Cmp ¶
Cmp compares x and y and returns -1, 0 or 1
-1 if x < y 0 if x == y +1 if x > y
func (Decimal) Div ¶
Returns d / d2. If it doesn't divide exactly, the result will have DivisionPrecision digits after the decimal point.
func (Decimal) 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 ¶
func (Decimal) MarshalText ¶
xml serialization
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) StringScaled ¶
StringScaled first scales the decimal then calls .String() on it.
func (Decimal) Truncate ¶
This truncates off digits from the number, without rounding
NOTE: precision is the last digit that will not be truncated (should be >= 0)
decimal.NewFromString("123.456").Truncate(2).String() // "123.45"
func (*Decimal) UnmarshalJSON ¶
func (*Decimal) UnmarshalText ¶
xml deserialization