decimal

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

README

Decimal

This package provides a decimal type that can represent decimal numbers with arbitrary precision. Currently, it uses cockroachdb/apd package under the hood.

Features

  • Decimal is safe to use without initialization, it's 0 by default.
  • Easy to use, it implements all the arithmetic operations.
  • JSON marshaling and unmarshalling support.
  • SQL scanning and value support.

Caveats

  • Most of the methods can panic. It's not ideal, but it's a trade-off to provider a simpler API.
  • Decimal.Div method uses a default precision of 32, which can be customized by setting a new value to DivPrecision variable.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DivisionPrecision uint32 = 32

DivisionPrecision is the default precision for division used for all the `Decimal.Div` operations. It can be changed by setting `decimal.DivisionPrecision` to a different value.

Functions

This section is empty.

Types

type Decimal

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

Decimal is an arbitrary-precision decimal.

func FromFloat64

func FromFloat64(f float64) (Decimal, error)

FromFloat64 creates a new Decimal from a float64.

func FromInt

func FromInt(i int64) Decimal

FromInt creates a new Decimal from an int64.

func FromStr

func FromStr(str string) (Decimal, error)

FromStr creates a new Decimal from a string.

func MustFromStr

func MustFromStr(str string) Decimal

MustFromStr creates a new Decimal from a string. It panics if the string is not a valid decimal.

func (Decimal) Add

func (d Decimal) Add(d2 Decimal) Decimal

Add adds two decimals and returns the result.

func (Decimal) Cmp

func (d Decimal) Cmp(d2 Decimal) int

Cmp compares two decimals and returns: -1 if d < d2 0 if d == d2 1 if d > d2

func (Decimal) DigitsAfterPeriod added in v0.0.3

func (d Decimal) DigitsAfterPeriod() int

DigitsAfterPeriod returns number of digits after the period.

func (Decimal) Div

func (d Decimal) Div(d2 Decimal) Decimal

Div divides the current decimal by a given one and returns the result.

func (Decimal) DivPrecision

func (d Decimal) DivPrecision(d2 Decimal, precision uint32) Decimal

DivPrecision divides the current decimal by a given one and returns the result with a given precision.

func (Decimal) Equal

func (d Decimal) Equal(d2 Decimal) bool

Equal returns true if d == d2.

func (Decimal) Float64

func (d Decimal) Float64() (float64, error)

Float64 returns the float64 representation of the decimal.

func (Decimal) Greater

func (d Decimal) Greater(d2 Decimal) bool

Greater returns true if d > d2.

func (Decimal) GreaterOrEqual

func (d Decimal) GreaterOrEqual(d2 Decimal) bool

GreaterOrEqual returns true if d >= d2.

func (Decimal) InRange

func (d Decimal) InRange(min, max Decimal) bool

InRange returns true if d is in the range of [min, max] decimals.

func (Decimal) InRangeInt

func (d Decimal) InRangeInt(min, max int64) bool

InRangeInt returns true if d is in the range of [min, max] integers.

func (*Decimal) IsNegative

func (d *Decimal) IsNegative() bool

IsNegative returns true if d < 0.

func (Decimal) IsZero

func (d Decimal) IsZero() bool

IsZero returns true if d == 0.

func (Decimal) Less

func (d Decimal) Less(d2 Decimal) bool

Less returns true if d < d2.

func (Decimal) LessOrEqual

func (d Decimal) LessOrEqual(d2 Decimal) bool

LessOrEqual returns true if d <= d2.

func (Decimal) MarshalText

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

MarshalText implements encoding.TextMarshaler.

func (Decimal) Mul

func (d Decimal) Mul(d2 Decimal) Decimal

Mul multiplies two decimals and returns the result.

func (Decimal) MulInt

func (d Decimal) MulInt(i int64) Decimal

MulInt multiplies a decimal by an int64 and returns the result.

func (Decimal) MustFloat64

func (d Decimal) MustFloat64() float64

MustFloat64 returns the float64 representation of the decimal. It panics if the decimal can't be converted to float64.

func (Decimal) Reduce

func (d Decimal) Reduce() Decimal

Reduce removes all the trailing zeroes from the decimal.

func (Decimal) Round

func (d Decimal) Round(n uint16) Decimal

Round rounds the decimal to n digits after 0.

func (*Decimal) RoundOrNil

func (d *Decimal) RoundOrNil(n uint16) *Decimal

RoundOrNil returns nil if the Decimal is nil or rounds it to n digits after 0.

func (*Decimal) Scan

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

Scan implements sql.Scanner.

func (Decimal) String

func (d Decimal) String() string

String returns the string representation of the decimal.

func (Decimal) Sub

func (d Decimal) Sub(d2 Decimal) Decimal

Sub subtracts two decimals and returns the result.

func (*Decimal) UnmarshalJSON

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

UnmarshalJSON implements the json.Unmarshaler interface.

func (*Decimal) UnmarshalText

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

UnmarshalText implements encoding.TextUnmarshaler.

func (Decimal) Value

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

Value implements driver.Valuer.

Jump to

Keyboard shortcuts

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