Documentation
¶
Index ¶
- Variables
- func RescalePair(d1 Decimal, d2 Decimal) (Decimal, Decimal)
- type Decimal
- func Avg(first Decimal, rest ...Decimal) Decimal
- func Max(first Decimal, rest ...Decimal) Decimal
- func Min(first Decimal, rest ...Decimal) Decimal
- func New(value int64, exp int32) Decimal
- func NewFromBigInt(value *big.Int, exp int32) Decimal
- func NewFromFloat(f float64) Decimal
- func NewFromFloat32(f float32) Decimal
- func NewFromFloatWithExponent(value float64, exp int32) Decimal
- func NewFromFormattedString(value string, replRegexp *regexp.Regexp) (Decimal, error)
- func NewFromInt(x int64) Decimal
- func NewFromInt32(value int32) Decimal
- func NewFromString(value string) (Decimal, error)
- func RequireFromString(value string) Decimal
- func Sum(first Decimal, rest ...Decimal) Decimal
- func (d Decimal) Abs() Decimal
- func (d Decimal) Add(d2 Decimal) Decimal
- func (d Decimal) Atan() Decimal
- func (d Decimal) BigFloat() *big.Float
- func (d Decimal) BigInt() *big.Int
- func (d Decimal) Ceil() Decimal
- func (d Decimal) Cmp(d2 Decimal) int
- func (d Decimal) Coefficient() *big.Int
- func (d Decimal) CoefficientInt64() int64
- func (d Decimal) Copy() Decimal
- func (d Decimal) Cos() Decimal
- func (d Decimal) Div(d2 Decimal) Decimal
- func (d Decimal) DivRound(d2 Decimal, precision int32) Decimal
- func (d Decimal) Equal(d2 Decimal) bool
- func (d Decimal) Equals(d2 Decimal) bool
- func (d Decimal) ExpHullAbrham(overallPrecision uint32) (Decimal, error)
- func (d Decimal) ExpTaylor(precision int32) (Decimal, error)
- func (d Decimal) Exponent() int32
- func (d Decimal) Float64() (f float64, exact bool)
- func (d Decimal) Floor() Decimal
- func (d Decimal) GetFallback() *decimal.Decimal
- func (d Decimal) GetFixed() int64
- func (d *Decimal) GobDecode(data []byte) error
- func (d Decimal) GobEncode() ([]byte, error)
- func (d Decimal) GreaterThan(d2 Decimal) bool
- func (d Decimal) GreaterThanOrEqual(d2 Decimal) bool
- func (d Decimal) InexactFloat64() float64
- func (d Decimal) IntPart() int64
- func (d Decimal) IsInteger() bool
- func (d Decimal) IsNegative() bool
- func (d Decimal) IsOptimized() bool
- func (d Decimal) IsPositive() bool
- func (d Decimal) IsZero() bool
- func (d Decimal) LessThan(d2 Decimal) bool
- func (d Decimal) LessThanOrEqual(d2 Decimal) bool
- func (d Decimal) MarshalBinary() (data []byte, err error)
- func (d Decimal) MarshalJSON() ([]byte, error)
- func (d Decimal) MarshalText() (text []byte, err error)
- func (d Decimal) Mod(d2 Decimal) Decimal
- func (d Decimal) Mul(d2 Decimal) Decimal
- func (d Decimal) Neg() Decimal
- func (d Decimal) NumDigits() int
- func (d Decimal) Pow(d2 Decimal) Decimal
- func (d Decimal) QuoRem(d2 Decimal, precision int32) (Decimal, Decimal)
- func (d Decimal) Rat() *big.Rat
- func (d Decimal) Round(places int32) Decimal
- func (d Decimal) RoundBank(places int32) Decimal
- func (d Decimal) RoundCash(interval uint8) Decimal
- func (d Decimal) RoundCeil(places int32) Decimal
- func (d Decimal) RoundDown(places int32) Decimal
- func (d Decimal) RoundFloor(places int32) Decimal
- func (d Decimal) RoundUp(places int32) Decimal
- func (d *Decimal) Scan(value interface{}) error
- func (d Decimal) Shift(shift int32) Decimal
- func (d Decimal) Sign() int
- func (d Decimal) Sin() Decimal
- func (d Decimal) String() string
- func (d Decimal) StringFixed(places int32) string
- func (d Decimal) StringFixedBank(places int32) string
- func (d Decimal) StringFixedCash(interval uint8) string
- func (d Decimal) StringScaled(exp int32) string
- func (d Decimal) Sub(d2 Decimal) Decimal
- func (d Decimal) Tan() Decimal
- func (d Decimal) Truncate(precision int32) Decimal
- func (d *Decimal) UnmarshalBinary(data []byte) error
- func (d *Decimal) UnmarshalJSON(decimalBytes []byte) error
- func (d *Decimal) UnmarshalText(text []byte) error
- func (d Decimal) Value() (driver.Value, error)
- type NullDecimal
- func (d NullDecimal) MarshalJSON() ([]byte, error)
- func (d NullDecimal) MarshalText() (text []byte, err error)
- func (d *NullDecimal) Scan(value interface{}) error
- func (d *NullDecimal) UnmarshalJSON(decimalBytes []byte) error
- func (d *NullDecimal) UnmarshalText(text []byte) error
- func (d NullDecimal) Value() (driver.Value, error)
Constants ¶
This section is empty.
Variables ¶
var ( DivisionPrecision = decimal.DivisionPrecision ExpMaxIterations = decimal.ExpMaxIterations MarshalJSONWithoutQuotes = decimal.MarshalJSONWithoutQuotes Zero = Decimal{/* contains filtered or unexported fields */} )
Variables
Functions ¶
Types ¶
type Decimal ¶
type Decimal struct {
// contains filtered or unexported fields
}
func NewFromBigInt ¶
fallback: NewFromBigInt returns a new Decimal from a big.Int, value * 10 ^ exp
func NewFromFloat ¶
optimized: NewFromFloat converts a float64 to Decimal.
NOTE: this will panic on NaN, +/-inf
func NewFromFloat32 ¶ added in v1.1.0
fallback: NewFromFloat32 converts a float32 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 6-8 digits depending on the input. 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 NewFromFloatWithExponent ¶
fallback: NewFromFloatWithExponent converts a float64 to Decimal, with an arbitrary number of fractional digits.
Example:
NewFromFloatWithExponent(123.456, -2).String() // output: "123.46"
func NewFromFormattedString ¶ added in v1.3.2
fallback: NewFromFormattedString returns a new Decimal from a formatted string representation. The second argument - replRegexp, is a regular expression that is used to find characters that should be removed from given decimal string representation. All matched characters will be replaced with an empty string.
func NewFromInt ¶ added in v1.3.2
optimized: NewFromInt converts a int64 to Decimal.
func NewFromInt32 ¶ added in v1.3.2
optimized: NewFromInt32 converts a int32 to Decimal.
func NewFromString ¶
optimized: NewFromString returns a new Decimal from a string representation.
func RequireFromString ¶ added in v1.0.1
optimized: RequireFromString returns a new Decimal from a string representation or panics if NewFromString would have returned an error.
func (Decimal) BigInt ¶ added in v1.3.2
fallback: BigInt returns integer component of the decimal as a BigInt.
func (Decimal) Cmp ¶
optimized: 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) Coefficient ¶
optimized: Coefficient returns the coefficient of the decimal. It is scaled by 10^Exponent()
func (Decimal) CoefficientInt64 ¶ added in v1.3.2
optimized: CoefficientInt64 returns the coefficient of the decimal as int64. It is scaled by 10^Exponent()
func (Decimal) Copy ¶ added in v1.3.2
optimized: Copy returns a copy of decimal with the same value and exponent, but a different pointer to value.
func (Decimal) Div ¶
optimized: Div returns d / d2. If it doesn't divide exactly, the result will have DivisionPrecision digits after the decimal point.
func (Decimal) Equal ¶
optimized: Equal returns whether the numbers represented by d and d2 are equal.
func (Decimal) ExpHullAbrham ¶ added in v1.3.2
fallback: ExpHullAbrham calculates the natural exponent of decimal (e to the power of d) using Hull-Abraham algorithm. OverallPrecision argument specifies the overall precision of the result (integer part + decimal part).
func (Decimal) ExpTaylor ¶ added in v1.3.2
fallback: ExpTaylor calculates the natural exponent of decimal (e to the power of d) using Taylor series expansion. Precision argument specifies how precise the result must be (number of digits after decimal point). Negative precision is allowed.
func (Decimal) Exponent ¶
optimized: Exponent returns the exponent, or scale component of the decimal.
func (Decimal) Float64 ¶
fallback: Float64 returns the nearest float64 value for d and a bool indicating whether f represents d exactly.
func (Decimal) GetFallback ¶ added in v1.4.0
func (Decimal) GetFixed ¶ added in v1.4.0
Extra API to support get internal state. e.g. might be useful for flatbuffers encode / decode.
func (Decimal) GreaterThan ¶
optimized: GreaterThan (GT) returns true when d is greater than d2.
func (Decimal) GreaterThanOrEqual ¶
optimized: GreaterThanOrEqual (GTE) returns true when d is greater than or equal to d2.
func (Decimal) InexactFloat64 ¶ added in v1.3.2
fallback: InexactFloat64 returns the nearest float64 value for d. It doesn't indicate if the returned value represents d exactly.
func (Decimal) IsInteger ¶ added in v1.3.2
optimized: IsInteger returns true when decimal can be represented as an integer value, otherwise, it returns false.
func (Decimal) IsNegative ¶ added in v1.1.0
optimized: IsNegative return
true if d < 0 false if d == 0 false if d > 0
func (Decimal) IsOptimized ¶ added in v1.4.0
func (Decimal) IsPositive ¶ added in v1.1.0
optimized: IsPositive return
true if d > 0 false if d == 0 false if d < 0
func (Decimal) IsZero ¶ added in v1.1.0
optimized: IsZero return
true if d == 0 false if d > 0 false if d < 0
func (Decimal) LessThanOrEqual ¶
optimized: LessThanOrEqual (LTE) returns true when d is less than or equal to d2.
func (Decimal) MarshalBinary ¶
fallback: MarshalBinary implements the encoding.BinaryMarshaler interface.
func (Decimal) NumDigits ¶ added in v1.3.2
fallback: NumDigits returns the number of digits of the decimal coefficient (d.Value)
func (Decimal) Round ¶
optimized: Round rounds the decimal to places decimal places. If places < 0, it will round the integer part to the nearest 10^(-places).
func (Decimal) RoundBank ¶
fallback: RoundBank rounds the decimal to places decimal places. If the final digit to round is equidistant from the nearest two integers the rounded value is taken as the even number
If places < 0, it will round the integer part to the nearest 10^(-places).
func (Decimal) RoundCash ¶
fallback: RoundCash aka Cash/Penny/öre rounding rounds decimal to a specific interval. The amount payable for a cash transaction is rounded to the nearest multiple of the minimum currency unit available. The following intervals are available: 5, 10, 25, 50 and 100; any other number throws a panic.
5: 5 cent rounding 3.43 => 3.45 10: 10 cent rounding 3.45 => 3.50 (5 gets rounded up) 25: 25 cent rounding 3.41 => 3.50 50: 50 cent rounding 3.75 => 4.00 100: 100 cent rounding 3.50 => 4.00
For more details: https://en.wikipedia.org/wiki/Cash_rounding
func (Decimal) RoundCeil ¶ added in v1.3.2
fallback: RoundCeil rounds the decimal towards +infinity.
Example:
NewFromFloat(545).RoundCeil(-2).String() // output: "600" NewFromFloat(500).RoundCeil(-2).String() // output: "500" NewFromFloat(1.1001).RoundCeil(2).String() // output: "1.11" NewFromFloat(-1.454).RoundCeil(1).String() // output: "-1.5"
func (Decimal) RoundDown ¶ added in v1.3.2
fallback: RoundDown rounds the decimal towards zero.
Example:
NewFromFloat(545).RoundDown(-2).String() // output: "500" NewFromFloat(-500).RoundDown(-2).String() // output: "-500" NewFromFloat(1.1001).RoundDown(2).String() // output: "1.1" NewFromFloat(-1.454).RoundDown(1).String() // output: "-1.5"
func (Decimal) RoundFloor ¶ added in v1.3.2
fallback: RoundFloor rounds the decimal towards -infinity.
Example:
NewFromFloat(545).RoundFloor(-2).String() // output: "500" NewFromFloat(-500).RoundFloor(-2).String() // output: "-500" NewFromFloat(1.1001).RoundFloor(2).String() // output: "1.1" NewFromFloat(-1.454).RoundFloor(1).String() // output: "-1.4"
func (Decimal) RoundUp ¶ added in v1.3.2
fallback: RoundUp rounds the decimal away from zero.
Example:
NewFromFloat(545).RoundUp(-2).String() // output: "600" NewFromFloat(500).RoundUp(-2).String() // output: "500" NewFromFloat(1.1001).RoundUp(2).String() // output: "1.11" NewFromFloat(-1.454).RoundUp(1).String() // output: "-1.4"
func (Decimal) String ¶
optimized: String returns the string representation of the decimal with the fixed point.
func (Decimal) StringFixed ¶
fallback: StringFixed returns a rounded fixed-point string with places digits after the decimal point.
func (Decimal) StringFixedBank ¶
fallback: StringFixedBank returns a banker rounded fixed-point string with places digits after the decimal point.
func (Decimal) StringFixedCash ¶
fallback: StringFixedCash returns a Swedish/Cash rounded fixed-point string. For more details see the documentation at function RoundCash.
func (Decimal) StringScaled ¶
fallback: DEPRECATED! Use StringFixed instead.
func (Decimal) Truncate ¶
optimized: Truncate truncates off digits from the number, without rounding.
func (*Decimal) UnmarshalBinary ¶
fallback: UnmarshalBinary implements the encoding.BinaryUnmarshaler interface. As a string representation is already used when encoding to text, this method stores that string as []byte
func (*Decimal) UnmarshalJSON ¶
optimized: UnmarshalJSON implements the json.Unmarshaler interface.
func (*Decimal) UnmarshalText ¶
optimized: UnmarshalText implements the encoding.TextUnmarshaler interface for XML deserialization.
type NullDecimal ¶
NullDecimal support
func NewNullDecimal ¶ added in v1.3.2
func NewNullDecimal(d Decimal) NullDecimal
func (NullDecimal) MarshalJSON ¶
func (d NullDecimal) MarshalJSON() ([]byte, error)
func (NullDecimal) MarshalText ¶ added in v1.3.2
func (d NullDecimal) MarshalText() (text []byte, err error)
func (*NullDecimal) Scan ¶
func (d *NullDecimal) Scan(value interface{}) error
func (*NullDecimal) UnmarshalJSON ¶
func (d *NullDecimal) UnmarshalJSON(decimalBytes []byte) error
func (*NullDecimal) UnmarshalText ¶ added in v1.3.2
func (d *NullDecimal) UnmarshalText(text []byte) error