Documentation ¶
Overview ¶
Package decimal implements an arbitrary precision fixed-point decimal.
The zero-value of a BigDecimal is 0, as you would expect.
The best way to create a new BigDecimal is to use decimal.NewFromString, ex:
n, err := decimal.NewFromString("-123.4567") n.String() // output: "-123.4567"
To use BigDecimal as part of a struct:
type Struct struct { Number BigDecimal }
Note: This can "only" represent numbers with a maximum of 2^31 digits after the decimal point.
Index ¶
- Variables
- func RescalePair(d1 BigDecimal, d2 BigDecimal) (BigDecimal, BigDecimal)
- type BigDecimal
- func Avg(first BigDecimal, rest ...BigDecimal) BigDecimal
- func Max(first BigDecimal, rest ...BigDecimal) BigDecimal
- func Min(first BigDecimal, rest ...BigDecimal) BigDecimal
- func New(value int64, exp int32) BigDecimal
- func NewFromBigInt(value *big.Int, exp int32) BigDecimal
- func NewFromFloat(value float64) BigDecimal
- func NewFromFloat32(value float32) BigDecimal
- func NewFromFloatWithExponent(value float64, exp int32) BigDecimal
- func NewFromFormattedString(value string, replRegexp *regexp.Regexp) (BigDecimal, error)
- func NewFromInt(value int64) BigDecimal
- func NewFromInt32(value int32) BigDecimal
- func NewFromString(value string) (BigDecimal, error)
- func RequireFromString(value string) BigDecimal
- func Sum(first BigDecimal, rest ...BigDecimal) BigDecimal
- func (d BigDecimal) Abs() BigDecimal
- func (d BigDecimal) Add(d2 BigDecimal) BigDecimal
- func (d BigDecimal) Atan() BigDecimal
- func (d BigDecimal) BigFloat() *big.Float
- func (d BigDecimal) BigInt() *big.Int
- func (d BigDecimal) Ceil() BigDecimal
- func (d BigDecimal) Cmp(d2 BigDecimal) int
- func (d BigDecimal) Coefficient() *big.Int
- func (d BigDecimal) CoefficientInt64() int64
- func (d BigDecimal) CompareTo(d2 BigDecimal) int
- func (d BigDecimal) Copy() BigDecimal
- func (d BigDecimal) Cos() BigDecimal
- func (d BigDecimal) Div(d2 BigDecimal) BigDecimal
- func (d BigDecimal) DivRound(d2 BigDecimal, precision int32) BigDecimal
- func (d BigDecimal) Equal(d2 BigDecimal) bool
- func (d BigDecimal) Equals(d2 BigDecimal) bool
- func (d BigDecimal) ExpHullAbrham(overallPrecision uint32) (BigDecimal, error)
- func (d BigDecimal) ExpTaylor(precision int32) (BigDecimal, error)
- func (d BigDecimal) Exponent() int32
- func (d BigDecimal) Float64() (f float64, exact bool)
- func (d BigDecimal) Floor() BigDecimal
- func (d *BigDecimal) GobDecode(data []byte) error
- func (d BigDecimal) GobEncode() ([]byte, error)
- func (d BigDecimal) GreaterThan(d2 BigDecimal) bool
- func (d BigDecimal) GreaterThanOrEqual(d2 BigDecimal) bool
- func (d BigDecimal) InexactFloat64() float64
- func (d BigDecimal) IntPart() int64
- func (d BigDecimal) IsInteger() bool
- func (d BigDecimal) IsNegative() bool
- func (d BigDecimal) IsPositive() bool
- func (d BigDecimal) IsZero() bool
- func (d BigDecimal) LessThan(d2 BigDecimal) bool
- func (d BigDecimal) LessThanOrEqual(d2 BigDecimal) bool
- func (d BigDecimal) MarshalBinary() (data []byte, err error)
- func (d BigDecimal) MarshalJSON() ([]byte, error)
- func (d BigDecimal) MarshalText() (text []byte, err error)
- func (d BigDecimal) Mod(d2 BigDecimal) BigDecimal
- func (d BigDecimal) Mul(d2 BigDecimal) BigDecimal
- func (d BigDecimal) Neg() BigDecimal
- func (d BigDecimal) NumDigits() int
- func (d BigDecimal) Pow(d2 BigDecimal) BigDecimal
- func (d BigDecimal) QuoRem(d2 BigDecimal, precision int32) (BigDecimal, BigDecimal)
- func (d BigDecimal) Rat() *big.Rat
- func (d BigDecimal) Round(places int32) BigDecimal
- func (d BigDecimal) RoundBank(places int32) BigDecimal
- func (d BigDecimal) RoundCash(interval uint8) BigDecimal
- func (d BigDecimal) RoundCeil(places int32) BigDecimal
- func (d BigDecimal) RoundDown(places int32) BigDecimal
- func (d BigDecimal) RoundFloor(places int32) BigDecimal
- func (d BigDecimal) RoundUp(places int32) BigDecimal
- func (d *BigDecimal) Scan(value interface{}) error
- func (d BigDecimal) Shift(shift int32) BigDecimal
- func (d BigDecimal) Sign() int
- func (d BigDecimal) Sin() BigDecimal
- func (d BigDecimal) String() string
- func (d BigDecimal) StringFixed(places int32) string
- func (d BigDecimal) StringFixedBank(places int32) string
- func (d BigDecimal) StringFixedCash(interval uint8) string
- func (d BigDecimal) StringScaled(exp int32) string
- func (d BigDecimal) Sub(d2 BigDecimal) BigDecimal
- func (d BigDecimal) Tan() BigDecimal
- func (d BigDecimal) Truncate(precision int32) BigDecimal
- func (d *BigDecimal) UnmarshalBinary(data []byte) error
- func (d *BigDecimal) UnmarshalJSON(decimalBytes []byte) error
- func (d *BigDecimal) UnmarshalText(text []byte) error
- func (d BigDecimal) Value() (driver.Value, error)
- type BigInteger
- 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)
Examples ¶
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 ExpMaxIterations = 1000
ExpMaxIterations specifies the maximum number of iterations needed to calculate precise natural exponent value using ExpHullAbrham method.
var MarshalJSONWithoutQuotes = false
MarshalJSONWithoutQuotes should be set to true if you want the decimal to be JSON marshaled as a number, instead of as a string. WARNING: this is dangerous for decimals with many digits, since many JSON unmarshallers (ex: Javascript's) will unmarshal JSON numbers to IEEE 754 double-precision floating point numbers, which means you can potentially silently lose precision.
var ONE = big.NewInt(1)
var TEN = big.NewInt(10)
var ZERO = New(0, 1)
var Zero = New(0, 1)
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 ¶ added in v1.3.2
func RescalePair(d1 BigDecimal, d2 BigDecimal) (BigDecimal, BigDecimal)
RescalePair rescales two decimals to common exponential value (minimal exp of both decimals)
Types ¶
type BigDecimal ¶ added in v1.3.2
type BigDecimal struct {
// contains filtered or unexported fields
}
BigDecimal represents a fixed-point decimal. It is immutable. number = value * 10 ^ exp
func Avg ¶
func Avg(first BigDecimal, rest ...BigDecimal) BigDecimal
Avg returns the average value of the provided first and rest Decimals
func Max ¶
func Max(first BigDecimal, rest ...BigDecimal) BigDecimal
Max returns the largest BigDecimal 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 ¶
func Min(first BigDecimal, rest ...BigDecimal) BigDecimal
Min returns the smallest BigDecimal 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 New ¶
func New(value int64, exp int32) BigDecimal
New returns a new fixed-point decimal, value * 10 ^ exp.
func NewFromBigInt ¶
func NewFromBigInt(value *big.Int, exp int32) BigDecimal
NewFromBigInt returns a new BigDecimal from a big.Int, value * 10 ^ exp
func NewFromFloat ¶
func NewFromFloat(value float64) BigDecimal
NewFromFloat converts a float64 to BigDecimal.
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
Example ¶
fmt.Println(NewFromFloat(123.123123123123).String()) fmt.Println(NewFromFloat(.123123123123123).String()) fmt.Println(NewFromFloat(-1e13).String())
Output: 123.123123123123 0.123123123123123 -10000000000000
func NewFromFloat32 ¶ added in v1.1.0
func NewFromFloat32(value float32) BigDecimal
NewFromFloat32 converts a float32 to BigDecimal.
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
Example ¶
fmt.Println(NewFromFloat32(123.123123123123).String()) fmt.Println(NewFromFloat32(.123123123123123).String()) fmt.Println(NewFromFloat32(-1e13).String())
Output: 123.12312 0.123123124 -10000000000000
func NewFromFloatWithExponent ¶
func NewFromFloatWithExponent(value float64, exp int32) BigDecimal
NewFromFloatWithExponent converts a float64 to BigDecimal, with an arbitrary number of fractional digits.
Example:
NewFromFloatWithExponent(123.456, -2).String() // output: "123.46"
func NewFromFormattedString ¶ added in v1.3.2
func NewFromFormattedString(value string, replRegexp *regexp.Regexp) (BigDecimal, error)
NewFromFormattedString returns a new BigDecimal 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.
Example:
r := regexp.MustCompile("[$,]") d1, err := NewFromFormattedString("$5,125.99", r) r2 := regexp.MustCompile("[_]") d2, err := NewFromFormattedString("1_000_000", r2) r3 := regexp.MustCompile("[USD\\s]") d3, err := NewFromFormattedString("5000 USD", r3)
func NewFromInt ¶ added in v1.3.2
func NewFromInt(value int64) BigDecimal
NewFromInt converts a int64 to BigDecimal.
Example:
NewFromInt(123).String() // output: "123" NewFromInt(-10).String() // output: "-10"
func NewFromInt32 ¶ added in v1.3.2
func NewFromInt32(value int32) BigDecimal
NewFromInt32 converts a int32 to BigDecimal.
Example:
NewFromInt(123).String() // output: "123" NewFromInt(-10).String() // output: "-10"
func NewFromString ¶
func NewFromString(value string) (BigDecimal, error)
NewFromString returns a new BigDecimal from a string representation. Trailing zeroes are not trimmed.
Example:
d, err := NewFromString("-123.45") d2, err := NewFromString(".0001") d3, err := NewFromString("1.47000")
func RequireFromString ¶ added in v1.0.1
func RequireFromString(value string) BigDecimal
RequireFromString returns a new BigDecimal from a string representation or panics if NewFromString would have returned an error.
Example:
d := RequireFromString("-123.45") d2 := RequireFromString(".0001")
func Sum ¶
func Sum(first BigDecimal, rest ...BigDecimal) BigDecimal
Sum returns the combined total of the provided first and rest Decimals
func (BigDecimal) Abs ¶ added in v1.3.2
func (d BigDecimal) Abs() BigDecimal
Abs returns the absolute value of the decimal.
func (BigDecimal) Add ¶ added in v1.3.2
func (d BigDecimal) Add(d2 BigDecimal) BigDecimal
Add returns d + d2.
func (BigDecimal) Atan ¶ added in v1.3.2
func (d BigDecimal) Atan() BigDecimal
Atan returns the arctangent, in radians, of x.
func (BigDecimal) BigFloat ¶ added in v1.3.2
func (d BigDecimal) BigFloat() *big.Float
BigFloat returns decimal as BigFloat. Be aware that casting decimal to BigFloat might cause a loss of precision.
func (BigDecimal) BigInt ¶ added in v1.3.2
func (d BigDecimal) BigInt() *big.Int
BigInt returns integer component of the decimal as a BigInt.
func (BigDecimal) Ceil ¶ added in v1.3.2
func (d BigDecimal) Ceil() BigDecimal
Ceil returns the nearest integer value greater than or equal to d.
func (BigDecimal) Cmp ¶ added in v1.3.2
func (d BigDecimal) Cmp(d2 BigDecimal) 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 (BigDecimal) Coefficient ¶ added in v1.3.2
func (d BigDecimal) Coefficient() *big.Int
Coefficient returns the coefficient of the decimal. It is scaled by 10^Exponent()
func (BigDecimal) CoefficientInt64 ¶ added in v1.3.2
func (d BigDecimal) CoefficientInt64() int64
CoefficientInt64 returns the coefficient of the decimal as int64. It is scaled by 10^Exponent() If coefficient cannot be represented in an int64, the result will be undefined.
func (BigDecimal) CompareTo ¶ added in v1.3.4
func (d BigDecimal) CompareTo(d2 BigDecimal) int
func (BigDecimal) Copy ¶ added in v1.3.2
func (d BigDecimal) Copy() BigDecimal
Copy returns a copy of decimal with the same value and exponent, but a different pointer to value.
func (BigDecimal) Cos ¶ added in v1.3.2
func (d BigDecimal) Cos() BigDecimal
Cos returns the cosine of the radian argument x.
func (BigDecimal) Div ¶ added in v1.3.2
func (d BigDecimal) Div(d2 BigDecimal) BigDecimal
Div returns d / d2. If it doesn't divide exactly, the result will have DivisionPrecision digits after the decimal point.
func (BigDecimal) DivRound ¶ added in v1.3.2
func (d BigDecimal) DivRound(d2 BigDecimal, precision int32) BigDecimal
DivRound divides and rounds to a given precision i.e. to an integer multiple of 10^(-precision)
for a positive quotient digit 5 is rounded up, away from 0 if the quotient is negative then digit 5 is rounded down, away from 0
Note that precision<0 is allowed as input.
func (BigDecimal) Equal ¶ added in v1.3.2
func (d BigDecimal) Equal(d2 BigDecimal) bool
Equal returns whether the numbers represented by d and d2 are equal.
func (BigDecimal) Equals ¶ added in v1.3.2
func (d BigDecimal) Equals(d2 BigDecimal) bool
Equals is deprecated, please use Equal method instead
func (BigDecimal) ExpHullAbrham ¶ added in v1.3.2
func (d BigDecimal) ExpHullAbrham(overallPrecision uint32) (BigDecimal, error)
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).
ExpHullAbrham is faster than ExpTaylor for small precision values, but it is much slower for large precision values.
Example:
NewFromFloat(26.1).ExpHullAbrham(2).String() // output: "220000000000" NewFromFloat(26.1).ExpHullAbrham(20).String() // output: "216314672147.05767284"
func (BigDecimal) ExpTaylor ¶ added in v1.3.2
func (d BigDecimal) ExpTaylor(precision int32) (BigDecimal, error)
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.
ExpTaylor is much faster for large precision values than ExpHullAbrham.
Example:
d, err := NewFromFloat(26.1).ExpTaylor(2).String() d.String() // output: "216314672147.06" NewFromFloat(26.1).ExpTaylor(20).String() d.String() // output: "216314672147.05767284062928674083" NewFromFloat(26.1).ExpTaylor(-10).String() d.String() // output: "220000000000"
func (BigDecimal) Exponent ¶ added in v1.3.2
func (d BigDecimal) Exponent() int32
Exponent returns the exponent, or scale component of the decimal.
func (BigDecimal) Float64 ¶ added in v1.3.2
func (d BigDecimal) Float64() (f float64, exact bool)
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 (BigDecimal) Floor ¶ added in v1.3.2
func (d BigDecimal) Floor() BigDecimal
Floor returns the nearest integer value less than or equal to d.
func (*BigDecimal) GobDecode ¶ added in v1.3.2
func (d *BigDecimal) GobDecode(data []byte) error
GobDecode implements the gob.GobDecoder interface for gob serialization.
func (BigDecimal) GobEncode ¶ added in v1.3.2
func (d BigDecimal) GobEncode() ([]byte, error)
GobEncode implements the gob.GobEncoder interface for gob serialization.
func (BigDecimal) GreaterThan ¶ added in v1.3.2
func (d BigDecimal) GreaterThan(d2 BigDecimal) bool
GreaterThan (GT) returns true when d is greater than d2.
func (BigDecimal) GreaterThanOrEqual ¶ added in v1.3.2
func (d BigDecimal) GreaterThanOrEqual(d2 BigDecimal) bool
GreaterThanOrEqual (GTE) returns true when d is greater than or equal to d2.
func (BigDecimal) InexactFloat64 ¶ added in v1.3.2
func (d BigDecimal) InexactFloat64() float64
InexactFloat64 returns the nearest float64 value for d. It doesn't indicate if the returned value represents d exactly.
func (BigDecimal) IntPart ¶ added in v1.3.2
func (d BigDecimal) IntPart() int64
IntPart returns the integer component of the decimal.
func (BigDecimal) IsInteger ¶ added in v1.3.2
func (d BigDecimal) IsInteger() bool
IsInteger returns true when decimal can be represented as an integer value, otherwise, it returns false.
func (BigDecimal) IsNegative ¶ added in v1.3.2
func (d BigDecimal) IsNegative() bool
IsNegative return
true if d < 0 false if d == 0 false if d > 0
func (BigDecimal) IsPositive ¶ added in v1.3.2
func (d BigDecimal) IsPositive() bool
IsPositive return
true if d > 0 false if d == 0 false if d < 0
func (BigDecimal) IsZero ¶ added in v1.3.2
func (d BigDecimal) IsZero() bool
IsZero return
true if d == 0 false if d > 0 false if d < 0
func (BigDecimal) LessThan ¶ added in v1.3.2
func (d BigDecimal) LessThan(d2 BigDecimal) bool
LessThan (LT) returns true when d is less than d2.
func (BigDecimal) LessThanOrEqual ¶ added in v1.3.2
func (d BigDecimal) LessThanOrEqual(d2 BigDecimal) bool
LessThanOrEqual (LTE) returns true when d is less than or equal to d2.
func (BigDecimal) MarshalBinary ¶ added in v1.3.2
func (d BigDecimal) MarshalBinary() (data []byte, err error)
MarshalBinary implements the encoding.BinaryMarshaler interface.
func (BigDecimal) MarshalJSON ¶ added in v1.3.2
func (d BigDecimal) MarshalJSON() ([]byte, error)
MarshalJSON implements the json.Marshaler interface.
func (BigDecimal) MarshalText ¶ added in v1.3.2
func (d BigDecimal) MarshalText() (text []byte, err error)
MarshalText implements the encoding.TextMarshaler interface for XML serialization.
func (BigDecimal) Mod ¶ added in v1.3.2
func (d BigDecimal) Mod(d2 BigDecimal) BigDecimal
Mod returns d % d2.
func (BigDecimal) Mul ¶ added in v1.3.2
func (d BigDecimal) Mul(d2 BigDecimal) BigDecimal
Mul returns d * d2.
func (BigDecimal) NumDigits ¶ added in v1.3.2
func (d BigDecimal) NumDigits() int
NumDigits returns the number of digits of the decimal coefficient (d.Value) Note: Current implementation is extremely slow for large decimals and/or decimals with large fractional part
func (BigDecimal) Pow ¶ added in v1.3.2
func (d BigDecimal) Pow(d2 BigDecimal) BigDecimal
Pow returns d to the power d2
func (BigDecimal) QuoRem ¶ added in v1.3.2
func (d BigDecimal) QuoRem(d2 BigDecimal, precision int32) (BigDecimal, BigDecimal)
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 (BigDecimal) Rat ¶ added in v1.3.2
func (d BigDecimal) Rat() *big.Rat
Rat returns a rational number representation of the decimal.
func (BigDecimal) Round ¶ added in v1.3.2
func (d BigDecimal) Round(places int32) BigDecimal
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 (BigDecimal) RoundBank ¶ added in v1.3.2
func (d BigDecimal) RoundBank(places int32) BigDecimal
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).
Examples:
NewFromFloat(5.45).RoundBank(1).String() // output: "5.4" NewFromFloat(545).RoundBank(-1).String() // output: "540" NewFromFloat(5.46).RoundBank(1).String() // output: "5.5" NewFromFloat(546).RoundBank(-1).String() // output: "550" NewFromFloat(5.55).RoundBank(1).String() // output: "5.6" NewFromFloat(555).RoundBank(-1).String() // output: "560"
func (BigDecimal) RoundCash ¶ added in v1.3.2
func (d BigDecimal) RoundCash(interval uint8) BigDecimal
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 (BigDecimal) RoundCeil ¶ added in v1.3.2
func (d BigDecimal) RoundCeil(places int32) BigDecimal
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 (BigDecimal) RoundDown ¶ added in v1.3.2
func (d BigDecimal) RoundDown(places int32) BigDecimal
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 (BigDecimal) RoundFloor ¶ added in v1.3.2
func (d BigDecimal) RoundFloor(places int32) BigDecimal
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 (BigDecimal) RoundUp ¶ added in v1.3.2
func (d BigDecimal) RoundUp(places int32) BigDecimal
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 (*BigDecimal) Scan ¶ added in v1.3.2
func (d *BigDecimal) Scan(value interface{}) error
Scan implements the sql.Scanner interface for database deserialization.
func (BigDecimal) Shift ¶ added in v1.3.2
func (d BigDecimal) Shift(shift int32) BigDecimal
Shift shifts the decimal in base 10. It shifts left when shift is positive and right if shift is negative. In simpler terms, the given value for shift is added to the exponent of the decimal.
func (BigDecimal) Sign ¶ added in v1.3.2
func (d BigDecimal) Sign() int
Sign returns:
-1 if d < 0 0 if d == 0 +1 if d > 0
func (BigDecimal) Sin ¶ added in v1.3.2
func (d BigDecimal) Sin() BigDecimal
Sin returns the sine of the radian argument x.
func (BigDecimal) String ¶ added in v1.3.2
func (d BigDecimal) 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 (BigDecimal) StringFixed ¶ added in v1.3.2
func (d BigDecimal) 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 (BigDecimal) StringFixedBank ¶ added in v1.3.2
func (d BigDecimal) StringFixedBank(places int32) string
StringFixedBank returns a banker rounded fixed-point string with places digits after the decimal point.
Example:
NewFromFloat(0).StringFixedBank(2) // output: "0.00" NewFromFloat(0).StringFixedBank(0) // output: "0" NewFromFloat(5.45).StringFixedBank(0) // output: "5" NewFromFloat(5.45).StringFixedBank(1) // output: "5.4" NewFromFloat(5.45).StringFixedBank(2) // output: "5.45" NewFromFloat(5.45).StringFixedBank(3) // output: "5.450" NewFromFloat(545).StringFixedBank(-1) // output: "540"
func (BigDecimal) StringFixedCash ¶ added in v1.3.2
func (d BigDecimal) StringFixedCash(interval uint8) string
StringFixedCash returns a Swedish/Cash rounded fixed-point string. For more details see the documentation at function RoundCash.
func (BigDecimal) StringScaled ¶ added in v1.3.2
func (d BigDecimal) StringScaled(exp int32) string
StringScaled first scales the decimal then calls .String() on it. NOTE: buggy, unintuitive, and DEPRECATED! Use StringFixed instead.
func (BigDecimal) Sub ¶ added in v1.3.2
func (d BigDecimal) Sub(d2 BigDecimal) BigDecimal
Sub returns d - d2.
func (BigDecimal) Tan ¶ added in v1.3.2
func (d BigDecimal) Tan() BigDecimal
Tan returns the tangent of the radian argument x.
func (BigDecimal) Truncate ¶ added in v1.3.2
func (d BigDecimal) Truncate(precision int32) BigDecimal
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 (*BigDecimal) UnmarshalBinary ¶ added in v1.3.2
func (d *BigDecimal) UnmarshalBinary(data []byte) error
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 (*BigDecimal) UnmarshalJSON ¶ added in v1.3.2
func (d *BigDecimal) UnmarshalJSON(decimalBytes []byte) error
UnmarshalJSON implements the json.Unmarshaler interface.
func (*BigDecimal) UnmarshalText ¶ added in v1.3.2
func (d *BigDecimal) UnmarshalText(text []byte) error
UnmarshalText implements the encoding.TextUnmarshaler interface for XML deserialization.
type BigInteger ¶ added in v1.3.3
type NullDecimal ¶
type NullDecimal struct { BigDecimal BigDecimal Valid bool }
NullDecimal represents a nullable decimal with compatibility for scanning null values from the database.
func NewNullDecimal ¶ added in v1.3.2
func NewNullDecimal(d BigDecimal) NullDecimal
func (NullDecimal) MarshalJSON ¶
func (d NullDecimal) MarshalJSON() ([]byte, error)
MarshalJSON implements the json.Marshaler interface.
func (NullDecimal) MarshalText ¶ added in v1.3.2
func (d NullDecimal) MarshalText() (text []byte, err error)
MarshalText implements the encoding.TextMarshaler interface for XML serialization.
func (*NullDecimal) Scan ¶
func (d *NullDecimal) Scan(value interface{}) error
Scan implements the sql.Scanner interface for database deserialization.
func (*NullDecimal) UnmarshalJSON ¶
func (d *NullDecimal) UnmarshalJSON(decimalBytes []byte) error
UnmarshalJSON implements the json.Unmarshaler interface.
func (*NullDecimal) UnmarshalText ¶ added in v1.3.2
func (d *NullDecimal) UnmarshalText(text []byte) error
UnmarshalText implements the encoding.TextUnmarshaler interface for XML deserialization