decimal

package
v0.0.31 Latest Latest
Warning

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

Go to latest
Published: Feb 29, 2024 License: MIT Imports: 5 Imported by: 45

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrUnsupportedDecimalNotation = errors.New("luno: unsupported decimal notation")

Functions

This section is empty.

Types

type Decimal

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

Decimal represents a decimal amount, internally stored as a big.Int.

func New

func New(i *big.Int, scale int) Decimal

New returns a new Decimal. scale specifies the units of i as an inverse power of 10. For example, a scale of 2 indicates that i is in units of 10^-2, and a value of i=25 would represent 25*10^-2=0.25. If scale=0, then the decimal is an integer.

func NewFromFloat64

func NewFromFloat64(f float64, scale int) Decimal

NewFromFloat64 returns a new Decimal with the given scale. The value is truncated towards 0.

func NewFromInt64

func NewFromInt64(i int64) Decimal

func NewFromString

func NewFromString(s string) (Decimal, error)

func Zero

func Zero() Decimal

Zero returns a Decimal representing 0, with precision 0.

func (Decimal) Add

func (d Decimal) Add(y Decimal) Decimal

Add adds y to d and returns the result. d is left unchanged.

func (Decimal) Cmp

func (d Decimal) Cmp(y Decimal) int

Cmp returns 1 if d>y, -1 if d<y or 0 if d==y.

func (Decimal) Div

func (d Decimal) Div(y Decimal, scale int) Decimal

Div divides d by y and returns the result in the provided scale. If the provided scale is too small for the result of d/y, the result is truncated towards 0. d is left unchanged.

func (Decimal) DivInt64

func (d Decimal) DivInt64(y int64) Decimal

DivInt64 divides d by y and returns the result. d is left unchanged.

func (Decimal) Float64

func (d Decimal) Float64() float64

Float64 converts the decimal d to a float64.

func (Decimal) MarshalJSON

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

MarshalJSON converts the Decimal to JSON bytes.

func (Decimal) Mul

func (d Decimal) Mul(y Decimal) Decimal

Mul multiplies d by y and returns the result. The result has a scale equal to the sum of d's and y's scales. d is left unchanged.

func (Decimal) MulInt64

func (d Decimal) MulInt64(y int64) Decimal

MulInt64 multiplies d by y and returns the result. d is left unchanged.

func (Decimal) Neg

func (d Decimal) Neg() Decimal

Neg returns the negative of d. d is left unchanged.

func (Decimal) QueryValue added in v0.0.13

func (d Decimal) QueryValue() string

func (Decimal) Sign

func (d Decimal) Sign() int

Sign returns -1 if d is negative, 1 if d is positive and 0 if d is zero.

func (Decimal) String

func (d Decimal) String() string

Strings returns a string representation of d.

func (Decimal) Sub

func (d Decimal) Sub(y Decimal) Decimal

Sub subtracts y from d and returns the result. d is left unchanged.

func (Decimal) ToScale

func (d Decimal) ToScale(scale int) Decimal

ToScale returns a Decimal representing the same value as d, but with the given scale. If scale is less than the Decimal's current scale, i.e. the new Decimal has fewer decimal points, the decimal is truncated (rounded towards 0). d is left unchanged.

func (*Decimal) UnmarshalJSON

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

UnmarshalJSON reads JSON bytes into a Decimal.

Jump to

Keyboard shortcuts

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