decimal

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2019 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package decimal 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

Constants

This section is empty.

Variables

View Source
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"
View Source
var Zero = New(0, 1)

Zero 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 F

func F(f float64) Decimal

F is a convenience function for NewFromFloat()

func I

func I(i int) Decimal

I is a convenience function for NewFromFloat() on integer

func Max

func Max(first Decimal, rest ...Decimal) Decimal

Returns the largest Decimal 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 Decimal, rest ...Decimal) Decimal

Returns the smallest Decimal 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 N

func N(s string) Decimal

N is a convenience function for NewFromString(), it panics if value is invalid

func New

func New(value int64, exp int32) Decimal

New returns a new fixed-point decimal, value * 10 ^ exp.

func NewFromFloat

func NewFromFloat(value float64) Decimal

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

func NewFromFloatWithExponent(value float64, exp int32) Decimal

NewFromFloatWithExponent converts a float64 to Decimal, with an arbitrary number of fractional digits.

Example:

NewFromFloatWithExponent(123.456, -2).String() // output: "123.46"

func NewFromString

func NewFromString(value string) (Decimal, error)

NewFromString returns a new Decimal from a string representation.

Example:

d, err := NewFromString("-123.45")
d2, err := NewFromString(".0001")

func (Decimal) Abs

func (d Decimal) Abs() Decimal

Abs returns the absolute value of the decimal.

func (Decimal) Add

func (d Decimal) Add(d2 Decimal) Decimal

Add returns d + d2.

func (Decimal) AddF

func (d Decimal) AddF(f float64) Decimal

AddF returns d + f, where d is Decimal, f is float64

func (Decimal) AddI

func (d Decimal) AddI(i int) Decimal

AddI returns d + i, where d is Decimal, i is int

func (Decimal) Ceil

func (d Decimal) Ceil() Decimal

Ceil returns the nearest integer value greater than or equal to d.

func (Decimal) Cmp

func (d Decimal) Cmp(d2 Decimal) 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 (Decimal) Copy

func (d Decimal) Copy() Decimal

Copy copies d and returns the copy

func (Decimal) Div

func (d Decimal) Div(d2 Decimal) Decimal

Div returns d / d2. If it doesn't divide exactly, the result will have DivisionPrecision digits after the decimal point.

func (Decimal) DivF

func (d Decimal) DivF(f float64) Decimal

DivF returns d / f, where d is Decimal, f is float64

func (Decimal) DivI

func (d Decimal) DivI(i int) Decimal

DivI returns d / i, where d is Decimal, i is int

func (Decimal) Eq

func (d Decimal) Eq(d2 Decimal) bool

Eq returns true id d == d2

func (Decimal) Equals

func (d Decimal) Equals(d2 Decimal) bool

Equals returns whether the numbers represented by d and d2 are equal.

func (Decimal) Exponent

func (d Decimal) Exponent() int32

Exponent returns the exponent, or scale component of the decimal.

func (Decimal) F

func (d Decimal) F() float64

F converts decimal to float64

func (Decimal) Float64

func (d Decimal) 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 (Decimal) Float64f

func (d Decimal) Float64f() float64

like Float64() function, but returns only first parameter

func (Decimal) Floor

func (d Decimal) Floor() Decimal

Floor returns the nearest integer value less than or equal to d.

func (Decimal) Ge

func (d Decimal) Ge(d2 Decimal) bool

Ge returns true id d >= d2

func (Decimal) Gt

func (d Decimal) Gt(d2 Decimal) bool

Gt returns true id d > d2

func (Decimal) I

func (d Decimal) I() int

I converts decimal to int

func (Decimal) IntPart

func (d Decimal) IntPart() int64

IntPart returns the integer component of the decimal.

func (Decimal) Le

func (d Decimal) Le(d2 Decimal) bool

Le returns true id d <= d2

func (Decimal) Lt

func (d Decimal) Lt(d2 Decimal) bool

Lt returns true id d < d2

func (Decimal) MarshalJSON

func (d Decimal) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (Decimal) MarshalText

func (d Decimal) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface for XML serialization.

func (Decimal) Mul

func (d Decimal) Mul(d2 Decimal) Decimal

Mul returns d * d2.

func (Decimal) MulF

func (d Decimal) MulF(f float64) Decimal

MulF returns d * f, where d is Decimal, f is float64

func (Decimal) MulI

func (d Decimal) MulI(i int) Decimal

MulI returns d * i, where d is Decimal, i is int

func (Decimal) Ne

func (d Decimal) Ne(d2 Decimal) bool

Ne returns true id d != d2

func (Decimal) Neg

func (d Decimal) Neg() Decimal

Neg returns -d

func (Decimal) P

func (d Decimal) P() *Decimal

P returns a pointer to d

func (Decimal) Rat

func (d Decimal) Rat() *big.Rat

Rat returns a rational number representation of the decimal.

func (Decimal) Round

func (d Decimal) Round(places int32) Decimal

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 (Decimal) S

func (d Decimal) S() string

S converts decimal to string

func (*Decimal) Scan

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

Scan implements the sql.Scanner interface for database deserialization.

func (Decimal) String

func (d Decimal) 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 (Decimal) StringFixed

func (d Decimal) 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 (Decimal) StringScaled

func (d Decimal) StringScaled(exp int32) string

NOTE: buggy, unintuitive, and DEPRECATED! Use StringFixed instead. StringScaled first scales the decimal then calls .String() on it.

func (Decimal) Sub

func (d Decimal) Sub(d2 Decimal) Decimal

Sub returns d - d2.

func (Decimal) SubF

func (d Decimal) SubF(f float64) Decimal

SubF returns d - f, where d is Decimal, f is float64

func (Decimal) SubI

func (d Decimal) SubI(i int) Decimal

SubI returns d - i, where d is Decimal, i is int

func (Decimal) Truncate

func (d Decimal) Truncate(precision int32) Decimal

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 (*Decimal) UnmarshalJSON

func (d *Decimal) UnmarshalJSON(decimalBytes []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (*Decimal) UnmarshalText

func (d *Decimal) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface for XML deserialization.

func (Decimal) Value

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

Value implements the driver.Valuer interface for database serialization.

type Decimals

type Decimals []Decimal

func (Decimals) Copy

func (decimals Decimals) Copy() Decimals

Copy copies a slice of decimals

func (Decimals) Equal

func (decimals Decimals) Equal(decimals2 Decimals, ordered bool) bool

Equal compares two slices, decimals and decimals2, of Decimal if ordered is true, comparing order also, otherwise order may differ if slices are equal according to parameter conditions, it returns true, otherwise returns false

func (Decimals) Len

func (decimals Decimals) Len() int

func (Decimals) Less

func (decimals Decimals) Less(i, j int) bool

func (Decimals) RemoveDuplicates

func (decimals Decimals) RemoveDuplicates() Decimals

RemoveDuplicates returns a slice decimals which don't contain duplicate elements from decimals slice

func (Decimals) Sort

func (decimals Decimals) Sort()

func (Decimals) Swap

func (decimals Decimals) Swap(i, j int)

Jump to

Keyboard shortcuts

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