Documentation
¶
Overview ¶
Package decimal implements the Dec and NullDec types suitable for financial and monetary calculations.
Example ¶
// 100000 at 6.5% for 20 years => 352364.51 var total, rate Dec total.SetString("100000") rate.SetString("6.5") // rate = (1 + rate/100)**20 rate.Div(&rate, New(100), 3) rate.Add(&rate, New(1)) rate.Power(&rate, 20) total.Mul(&total, &rate) total.Round(2) fmt.Println(total)
Output: 352364.51
Index ¶
- type Dec
- func (d *Dec) Abs(x *Dec) *Dec
- func (d *Dec) Add(x, y *Dec) *Dec
- func (d Dec) Bytes() []byte
- func (x Dec) Cmp(y *Dec) int
- func (d *Dec) Div(x, y *Dec, scale uint8) *Dec
- func (d Dec) Float64() float64
- func (d *Dec) Mul(x, y *Dec) *Dec
- func (d *Dec) Neg(x *Dec) *Dec
- func (d *Dec) Power(x *Dec, n int) *Dec
- func (d *Dec) Round(scale uint8) *Dec
- func (d *Dec) Scan(value interface{}) error
- func (d *Dec) Set(x *Dec) *Dec
- func (d *Dec) SetBytes(buf []byte) error
- func (d *Dec) SetFloat64(f float64) error
- func (d *Dec) SetInt128(x *Int128) *Dec
- func (d *Dec) SetInt64(i int64) *Dec
- func (d *Dec) SetString(s string) error
- func (d Dec) Sign() int
- func (d Dec) String() string
- func (d *Dec) Sub(x, y *Dec) *Dec
- func (d Dec) Value() (driver.Value, error)
- type Int128
- func (z *Int128) Abs(x *Int128) *Int128
- func (z *Int128) Add(x, y *Int128) *Int128
- func (x *Int128) Bit(i int) uint
- func (i Int128) Bytes() []byte
- func (x Int128) Cmp(y *Int128) int
- func (z *Int128) Div(x, y *Int128) *Int128
- func (z *Int128) DivMod(x, y, r *Int128) (*Int128, *Int128)
- func (x Int128) Float64() float64
- func (x Int128) Int64() int64
- func (z *Int128) Lsh(x *Int128, n uint) *Int128
- func (z *Int128) Mod(x, y *Int128) *Int128
- func (z *Int128) Mul(x, y *Int128) *Int128
- func (z *Int128) Neg(x *Int128) *Int128
- func (z *Int128) Power(x *Int128, n uint) *Int128
- func (z *Int128) Rsh(x *Int128, n uint) *Int128
- func (z *Int128) Set(x *Int128) *Int128
- func (z *Int128) SetBit(x *Int128, i int, b uint) *Int128
- func (z *Int128) SetInt64(x int64) *Int128
- func (x Int128) Sign() int
- func (i Int128) String() string
- func (z *Int128) Sub(x, y *Int128) *Int128
- type NullDec
- func (d *NullDec) Abs(x *NullDec) *NullDec
- func (d *NullDec) Add(x, y *NullDec) *NullDec
- func (d NullDec) Bytes() []byte
- func (x NullDec) Cmp(y *NullDec) int
- func (d *NullDec) Dec() *Dec
- func (d *NullDec) Div(x, y *NullDec, scale uint8) *NullDec
- func (d NullDec) Float64() float64
- func (d *NullDec) Mul(x, y *NullDec) *NullDec
- func (d *NullDec) Neg(x *NullDec) *NullDec
- func (d NullDec) Null() bool
- func (d *NullDec) Power(x *NullDec, n int) *NullDec
- func (d *NullDec) Round(scale uint8) *NullDec
- func (d *NullDec) Scan(value interface{}) error
- func (d *NullDec) Set(x *NullDec) *NullDec
- func (d *NullDec) SetBytes(buf []byte) error
- func (d *NullDec) SetDec(x *Dec) *NullDec
- func (d *NullDec) SetFloat64(f float64) error
- func (d *NullDec) SetInt128(x *Int128) *NullDec
- func (d *NullDec) SetInt64(i int64) *NullDec
- func (d *NullDec) SetNull() *NullDec
- func (d *NullDec) SetString(s string) error
- func (d NullDec) Sign() int
- func (d NullDec) String() string
- func (d *NullDec) Sub(x, y *NullDec) *NullDec
- func (d NullDec) Value() (driver.Value, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Dec ¶
type Dec struct {
// contains filtered or unexported fields
}
Dec is represented as an 128 bit integer scaled by a power of ten.
func (*Dec) Add ¶
Add sets d to the sum x+y and returns d. The scale of d is the larger of the scales of the two operands.
Example ¶
var x, y Dec x.SetString("0.1") y.SetInt64(1) x.Add(&x, &y) fmt.Println(x)
Output: 1.1
func (Dec) Cmp ¶
Cmp compares x and y and returns:
-1 if x < y 0 if x == y +1 if x > y
Example ¶
var x, y Dec x.SetString("1.1") y.SetString("2.0") if x.Cmp(&y) < 0 { fmt.Printf("%s < %s", x, y) }
Output: 1.1 < 2.0
func (*Dec) Div ¶
Div sets d to the rounded quotient x/y and returns d. If y is zero panics with Division by zero. The resulting value is rounded half up to the given scale.
Example ¶
var x, y Dec x.SetInt64(100) y.SetInt64(3) x.Div(&x, &y, 2) fmt.Println(x)
Output: 33.33
func (*Dec) Mul ¶
Mul sets d to the product x*y and returns d. The scale of d is the sum of the scales of the two operands.
Example ¶
var x, y Dec x.SetString("1.1") y.SetInt64(2) x.Mul(&x, &y) fmt.Println(x)
Output: 2.2
func (*Dec) Neg ¶
Neg sets d to -x and returns d.
Example ¶
var d Dec d.SetString("12.34") d.Neg(&d) fmt.Println(d)
Output: -12.34
func (*Dec) SetBytes ¶
SetBytes sets d to the value of buf
Example ¶
var d Dec bytes := []byte("+12.34") d.SetBytes(bytes) fmt.Println(d)
Output: 12.34
func (*Dec) SetFloat64 ¶
SetFloat64 sets d to the value of f
func (*Dec) SetInt128 ¶
SetInt128 sets d to x and returns d.
Example ¶
var i Int128 i.SetInt64(100000) var d Dec d.SetInt128(&i) fmt.Println(d)
Output: 100000
func (*Dec) SetString ¶
SetString sets d to the value of s
Example ¶
var d Dec d.SetString("-12.34") fmt.Println(d)
Output: -12.34
type Int128 ¶
type Int128 struct {
// contains filtered or unexported fields
}
Int128 is a 128 bit signed integer.
Example ¶
var i Int128 i.SetInt64(-1) fmt.Println(i)
Output: -1
func (*Int128) DivMod ¶
DivMod sets z to the quotient x/y and r to the modulus x%y and returns the pair (z, r) for y != 0. If y == 0, a division-by-zero run-time panic occurs.
func (Int128) Int64 ¶
Int64 returns the int64 representation of x. If x cannot be represented in an int64, the result is undefined.
func (*Int128) Lsh ¶
Lsh sets z = x << n and returns z.
Example ¶
var i Int128 i.SetInt64(1) i.Lsh(&i, 64) fmt.Println(i)
Output: 18446744073709551616
func (*Int128) Rsh ¶
Rsh sets z = x >> n and returns z.
Example ¶
var i Int128 i.SetBit(&i, 64, 1) i.Rsh(&i, 64) fmt.Println(i)
Output: 1
func (*Int128) SetBit ¶
SetBit sets z to x, with x's i'th bit set to b (0 or 1). That is, if b is 1 SetBit sets z = x | (1 << i); if b is 0 SetBit sets z = x &^ (1 << i). If b is not 0 or 1, SetBit will panic.
type NullDec ¶
type NullDec struct {
// contains filtered or unexported fields
}
NullDec represents a decimal that may be null.
func (*NullDec) Add ¶
Add sets d to the sum x+y and returns d. The scale of d is the larger of the scales of the two operands.
Example ¶
var x, y NullDec x.SetString("0.1") y.SetInt64(1) x.Add(&x, &y) fmt.Println(x)
Output: 1.1
func (NullDec) Cmp ¶
Cmp compares x and y and returns:
-1 if x < y 0 if x == y +1 if x > y
Example ¶
var x, y NullDec x.SetString("1.1") y.SetString("2.0") if x.Cmp(&y) < 0 { fmt.Printf("%s < %s", x, y) }
Output: 1.1 < 2.0
func (*NullDec) Dec ¶
Dec returns d.
Example ¶
var d NullDec d.SetString("1.2") fmt.Println(d.Dec())
Output: 1.2
func (*NullDec) Div ¶
Div sets d to the rounded quotient x/y and returns d. If y is zero panics with Division by zero. The resulting value is rounded half up to the given scale.
Example ¶
var x, y NullDec x.SetInt64(100) y.SetInt64(3) x.Div(&x, &y, 2) fmt.Println(x)
Output: 33.33
func (*NullDec) Mul ¶
Mul sets d to the product x*y and returns d. The scale of d is the sum of the scales of the two operands.
Example ¶
var x, y NullDec x.SetString("1.1") y.SetInt64(2) x.Mul(&x, &y) fmt.Println(x)
Output: 2.2
func (*NullDec) Neg ¶
Neg sets d to -x and returns d.
Example ¶
var d NullDec d.SetString("12.34") d.Neg(&d) fmt.Println(d)
Output: -12.34
func (NullDec) Null ¶
Null returns true if the value of d is null
Example ¶
var d NullDec d.SetString("-12.34") if !d.Null() { fmt.Println(d) } d.SetString("") if d.Null() { fmt.Println("null") }
Output: -12.34 null
func (*NullDec) SetBytes ¶
SetBytes sets d to the value of buf
Example ¶
var d NullDec bytes := []byte("-12.34") d.SetBytes(bytes) fmt.Println(d)
Output: -12.34
func (*NullDec) SetDec ¶
SetDec sets d to x and returns d.
Example ¶
var d Dec d.SetString("1.2") var nd NullDec nd.SetDec(&d) fmt.Println(nd)
Output: 1.2
func (*NullDec) SetFloat64 ¶
SetFloat64 sets d to the value of f
func (*NullDec) SetInt128 ¶
SetInt128 sets d to the value of x and returns d.
Example ¶
var i Int128 i.SetInt64(100000) var d NullDec d.SetInt128(&i) fmt.Println(d)
Output: 100000
func (*NullDec) SetString ¶
SetString sets d to the value of s
Example ¶
var d NullDec d.SetString("12.34") fmt.Println(d)
Output: 12.34
func (NullDec) String ¶
String returns the value of d. Returns an empty string if the value is null.