decimal

package module
v0.0.0-...-2615572 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 19, 2016 License: BSD-2-Clause Imports: 7 Imported by: 2

README

decimal

go decimal package suitable for financial and monetary calculations

Build Status

Installation

go get github.com/dimdin/decimal

Documentation

Documentation and usage examples

Usage

import "github.com/dimdin/decimal"

    var x, y decimal.Dec
    x.SetString("100")
    y.SetString("3")
    x.Div(&x, &y, 2)
    fmt.Println(x)

Output:

33.33

Features

  • 38 decimal digits precision implemented with an 128 bit integer scaled by a power of ten.
  • Fast addition, subtraction, multiplication, division and power operations.
  • Arithmetic overflow detection that panics.
  • Can be scanned directly from database/sql query results.
  • Can be used directly in database/sql Query and Exec parameters.
  • Methods are in the math/big form func (z *Dec) Op(x, y *Dec) *Dec with the result as receiver.
  • Arithmetic half up rounding.
  • Test suite with more than 90% code coverage.

License

Use of this source code is governed by BSD 2-clause license that can be found in the LICENSE file.

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

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 New

func New(n int64) *Dec

func (*Dec) Abs

func (d *Dec) Abs(x *Dec) *Dec

Abs sets d to |x| (the absolute value of x) and returns d.

func (*Dec) Add

func (d *Dec) Add(x, y *Dec) *Dec

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) Bytes

func (d Dec) Bytes() []byte

Bytes returns the value of d

func (Dec) Cmp

func (x Dec) Cmp(y *Dec) int

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

func (d *Dec) Div(x, y *Dec, scale uint8) *Dec

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) Float64

func (d Dec) Float64() float64

Float64 returns the nearest float64 representation of d.

func (*Dec) Mul

func (d *Dec) Mul(x, y *Dec) *Dec

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

func (d *Dec) Neg(x *Dec) *Dec

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) Power

func (d *Dec) Power(x *Dec, n int) *Dec

Power sets d = x**n and returns d

func (*Dec) Round

func (d *Dec) Round(scale uint8) *Dec

Round d half up to the given scale and returns d

func (*Dec) Scan

func (d *Dec) Scan(value interface{}) error

Scan implements the database Scanner interface.

func (*Dec) Set

func (d *Dec) Set(x *Dec) *Dec

Set sets d to x and returns d.

func (*Dec) SetBytes

func (d *Dec) SetBytes(buf []byte) error

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

func (d *Dec) SetFloat64(f float64) error

SetFloat64 sets d to the value of f

func (*Dec) SetInt128

func (d *Dec) SetInt128(x *Int128) *Dec

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) SetInt64

func (d *Dec) SetInt64(i int64) *Dec

SetInt64 sets d to the value of i and returns d.

func (*Dec) SetString

func (d *Dec) SetString(s string) error

SetString sets d to the value of s

Example
var d Dec
d.SetString("-12.34")
fmt.Println(d)
Output:

-12.34

func (Dec) Sign

func (d Dec) Sign() int

Sign returns:

-1 if d <  0
 0 if d == 0
+1 if d >  0

func (Dec) String

func (d Dec) String() string

String returns the value of d

func (*Dec) Sub

func (d *Dec) Sub(x, y *Dec) *Dec

Sub sets d to the difference x-y and returns d. The scale of d is the larger of the scales of the two operands.

func (Dec) Value

func (d Dec) Value() (driver.Value, error)

Value implements the database driver Valuer interface.

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) Abs

func (z *Int128) Abs(x *Int128) *Int128

Abs sets z to |x| (the absolute value of x) and returns z.

func (*Int128) Add

func (z *Int128) Add(x, y *Int128) *Int128

Add sets z to the sum x+y and returns z.

func (*Int128) Bit

func (x *Int128) Bit(i int) uint

Bit returns the value of the i'th bit of x. That is, it returns (x>>i)&1.

func (Int128) Bytes

func (i Int128) Bytes() []byte

Bytes returns the value of i

func (Int128) Cmp

func (x Int128) Cmp(y *Int128) int

Cmp compares x and y and returns:

-1 if x <  y
 0 if x == y
+1 if x >  y

func (*Int128) Div

func (z *Int128) Div(x, y *Int128) *Int128

Div sets z to the quotient x/y and returns z.

func (*Int128) DivMod

func (z *Int128) DivMod(x, y, r *Int128) (*Int128, *Int128)

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) Float64

func (x Int128) Float64() float64

Float64 returns the nearest float64 representation of x.

func (Int128) Int64

func (x Int128) Int64() int64

Int64 returns the int64 representation of x. If x cannot be represented in an int64, the result is undefined.

func (*Int128) Lsh

func (z *Int128) Lsh(x *Int128, n uint) *Int128

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) Mod

func (z *Int128) Mod(x, y *Int128) *Int128

Mod sets z to the modulus x/y and returns z.

func (*Int128) Mul

func (z *Int128) Mul(x, y *Int128) *Int128

Mul sets z to the product x*y and returns z.

func (*Int128) Neg

func (z *Int128) Neg(x *Int128) *Int128

Neg sets z to -x and returns z.

func (*Int128) Power

func (z *Int128) Power(x *Int128, n uint) *Int128

Power sets z = x^n and returns z

func (*Int128) Rsh

func (z *Int128) Rsh(x *Int128, n uint) *Int128

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) Set

func (z *Int128) Set(x *Int128) *Int128

Set sets z to x and returns z.

func (*Int128) SetBit

func (z *Int128) SetBit(x *Int128, i int, b uint) *Int128

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.

func (*Int128) SetInt64

func (z *Int128) SetInt64(x int64) *Int128

SetInt64 sets z to x and returns z.

func (Int128) Sign

func (x Int128) Sign() int

Sign returns:

-1 if x <  0
 0 if x == 0
+1 if x >  0

func (Int128) String

func (i Int128) String() string

String returns the value of i

func (*Int128) Sub

func (z *Int128) Sub(x, y *Int128) *Int128

Sub sets z to the difference x-y and returns z.

type NullDec

type NullDec struct {
	// contains filtered or unexported fields
}

NullDec represents a decimal that may be null.

func (*NullDec) Abs

func (d *NullDec) Abs(x *NullDec) *NullDec

Abs sets d to |x| (the absolute value of x) and returns d.

func (*NullDec) Add

func (d *NullDec) Add(x, y *NullDec) *NullDec

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) Bytes

func (d NullDec) Bytes() []byte

Bytes returns the value of d. Returns nil if the value is null.

func (NullDec) Cmp

func (x NullDec) Cmp(y *NullDec) int

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

func (d *NullDec) Dec() *Dec

Dec returns d.

Example
var d NullDec
d.SetString("1.2")
fmt.Println(d.Dec())
Output:

1.2

func (*NullDec) Div

func (d *NullDec) Div(x, y *NullDec, scale uint8) *NullDec

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) Float64

func (d NullDec) Float64() float64

Float64 returns the nearest float64 representation of d.

func (*NullDec) Mul

func (d *NullDec) Mul(x, y *NullDec) *NullDec

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

func (d *NullDec) Neg(x *NullDec) *NullDec

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

func (d NullDec) Null() bool

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) Power

func (d *NullDec) Power(x *NullDec, n int) *NullDec

Power sets d = x^n and returns d

func (*NullDec) Round

func (d *NullDec) Round(scale uint8) *NullDec

Round d half up to the given scale and returns d

func (*NullDec) Scan

func (d *NullDec) Scan(value interface{}) error

Scan implements the database Scanner interface.

func (*NullDec) Set

func (d *NullDec) Set(x *NullDec) *NullDec

Set sets d to x and returns d.

func (*NullDec) SetBytes

func (d *NullDec) SetBytes(buf []byte) error

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

func (d *NullDec) SetDec(x *Dec) *NullDec

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

func (d *NullDec) SetFloat64(f float64) error

SetFloat64 sets d to the value of f

func (*NullDec) SetInt128

func (d *NullDec) SetInt128(x *Int128) *NullDec

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) SetInt64

func (d *NullDec) SetInt64(i int64) *NullDec

SetInt64 sets d to the value of i and returns d.

func (*NullDec) SetNull

func (d *NullDec) SetNull() *NullDec

SetNull sets null to d and returns d.

func (*NullDec) SetString

func (d *NullDec) SetString(s string) error

SetString sets d to the value of s

Example
var d NullDec
d.SetString("12.34")
fmt.Println(d)
Output:

12.34

func (NullDec) Sign

func (d NullDec) Sign() int

Sign returns:

-1 if d <  0
 0 if d == 0
+1 if d >  0

func (NullDec) String

func (d NullDec) String() string

String returns the value of d. Returns an empty string if the value is null.

func (*NullDec) Sub

func (d *NullDec) Sub(x, y *NullDec) *NullDec

Sub sets d to the difference x-y and returns d. The scale of d is the larger of the scales of the two operands.

func (NullDec) Value

func (d NullDec) Value() (driver.Value, error)

Value implements the database driver Valuer interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL