big

package module
v0.7.22 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2021 License: MIT Imports: 7 Imported by: 2

README

BIG

Big is a simple, immuatable wrapper around golang's built-in *big.Float type desinged to offer a more user-friendly API and immutability guarantees at the cost of some runtime performance.

Example

Usage is dead simple:

dec := big.NewDecimal(1.24)
addend := big.NewDecimal(3.14)

dec.Add(addend).String() // prints "4.38"

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// NaN == Not a Number
	NaN = NewDecimal(math.NaN())

	// ZERO == 0
	ZERO = NewFromString("0")

	// ONE == 1
	ONE = NewFromString("1")

	// TEN == 10
	TEN = NewFromString("10")

	// MarshalQuoted - can toggle this to true to marshal values as strings
	MarshalQuoted = false
)

Functions

func FloatFromPool

func FloatFromPool() *big.Float

Types

type Decimal

type Decimal struct {
	Fl *big.Float
}

Decimal is the main exported type. It is a simple, immutable wrapper around a *big.Float

func DecimalFromPool

func DecimalFromPool() Decimal

func MaxSlice

func MaxSlice(decimals ...Decimal) Decimal

MaxSlice returns the max of a slice of decimals

func MinSlice

func MinSlice(decimals ...Decimal) Decimal

MinSlice returns the min of a slice of decimals

func NewDecimal

func NewDecimal(val float64) Decimal

NewDecimal creates a new Decimal type from a float value.

func NewFromInt

func NewFromInt(dec int) Decimal

NewFromInt creates a new Decimal type from an int value

func NewFromString

func NewFromString(str string) Decimal

NewFromString creates a new Decimal type from a string value.

func (Decimal) Abs

func (d Decimal) Abs() Decimal

Abs returns the absolute value of this Decimal

func (Decimal) Add

func (d Decimal) Add(addend Decimal) Decimal

Add adds a decimal instance to another Decimal instance.

func (Decimal) Cmp

func (d Decimal) Cmp(other Decimal) int

Cmp will return 1 if this decimal is greater than the provided, 0 if they are the same, and -1 if it is less.

func (Decimal) Div

func (d Decimal) Div(denominator Decimal) Decimal

Div divides this Decimal by the denominator passed.

func (Decimal) EQ

func (d Decimal) EQ(other Decimal) bool

EQ returns true if this Decimal exactly equals the provided decimal.

func (Decimal) Float

func (d Decimal) Float() float64

Float will return this Decimal as a float value. Note that there may be some loss of precision in this operation.

func (Decimal) FormattedString

func (d Decimal) FormattedString(places int) string

FormattedString returns the string value of the number to the requested precision

func (Decimal) Frac

func (d Decimal) Frac(f float64) Decimal

Frac returns another Decimal instance representing this Decimal multiplied by the provided float.

func (Decimal) GT

func (d Decimal) GT(other Decimal) bool

GT returns true if this decimal is greater than the provided decimal.

func (Decimal) GTE

func (d Decimal) GTE(other Decimal) bool

GTE returns true if this decimal is greater than or equal to the provided decimal.

func (Decimal) IsZero

func (d Decimal) IsZero() bool

IsZero will return true if this Decimal is equal to 0.

func (Decimal) LT

func (d Decimal) LT(other Decimal) bool

LT returns true if this decimal is less than the provided decimal.

func (Decimal) LTE

func (d Decimal) LTE(other Decimal) bool

LTE returns true if this decimal is less or equal to the provided decimal.

func (Decimal) MarshalJSON

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

MarshalJSON implements the json.Marshaler interface

func (Decimal) Mul

func (d Decimal) Mul(factor Decimal) Decimal

Mul multiplies another decimal instance with this Decimal instance.

func (Decimal) NaN

func (d Decimal) NaN() bool

NaN returns true if the underlying is not a valid number

func (Decimal) Neg

func (d Decimal) Neg() Decimal

Neg returns this Decimal multiplied by -1.

func (Decimal) Pow

func (d Decimal) Pow(exp int) Decimal

Pow returns the decimal to the inputted power

func (Decimal) ReturnToPool

func (d Decimal) ReturnToPool()

func (*Decimal) Scan

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

Scan implements the sql.Scanner interface

func (Decimal) Sqrt

func (d Decimal) Sqrt() Decimal

Sqrt returns the decimal's square root

func (Decimal) String

func (d Decimal) String() string

func (Decimal) Sub

func (d Decimal) Sub(subtrahend Decimal) Decimal

Sub subtracts another decimal instance from this Decimal instance.

func (*Decimal) UnmarshalJSON

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

UnmarshalJSON implements the json.Unmarshaler interface

func (Decimal) Value

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

Value implements the sql.Valuer interface

func (Decimal) Zero

func (d Decimal) Zero() bool

Zero will return true if this Decimal is equal to 0. Deprecated: Use IsZero instead

Jump to

Keyboard shortcuts

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