decimal

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ZERO is a Decimal with value 0
	ZERO = New(0)
	// ONE is a Decimal with value 1
	ONE = New(1)
)

Functions

This section is empty.

Types

type Decimal

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

Decimal represents a high-precision decimal number. It wraps math/big.Float to provide convenient methods for financial calculations.

func New

func New(f float64) Decimal

New creates a new Decimal from a float64

func NewFromBigFloat

func NewFromBigFloat(f *big.Float) Decimal

NewFromBigFloat creates a new Decimal from a big.Float

func NewFromInt

func NewFromInt(i int64) Decimal

NewFromInt creates a new Decimal from an int64

func NewFromString

func NewFromString(s string) Decimal

NewFromString creates a new Decimal from a string. It panics if string is not a valid number.

func NewFromStringWithError

func NewFromStringWithError(s string) (Decimal, error)

NewFromStringWithError creates a new Decimal from a string. Returns error if string is not a valid number.

func (Decimal) Abs

func (d Decimal) Abs() Decimal

Abs returns absolute value of d

func (Decimal) Add

func (d Decimal) Add(d2 Decimal) Decimal

Add returns d + d2

func (Decimal) Ceil

func (d Decimal) Ceil() Decimal

Ceil returns the least integer value greater than or equal to d

func (Decimal) Cmp

func (d Decimal) Cmp(d2 Decimal) int

Cmp compares d and d2 and returns:

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

func (Decimal) Div

func (d Decimal) Div(d2 Decimal) Decimal

Div returns d / d2

func (Decimal) EQ

func (d Decimal) EQ(d2 Decimal) bool

EQ returns true if d == d2

func (Decimal) Float

func (d Decimal) Float() float64

Float returns float64 representation of d

func (Decimal) Floor

func (d Decimal) Floor() Decimal

Floor returns the greatest integer value less than or equal to d

func (Decimal) FormattedString

func (d Decimal) FormattedString(precision int) string

FormattedString returns string representation of d with fixed precision

func (Decimal) Frac

func (d Decimal) Frac() Decimal

Frac returns the fractional part of d

func (Decimal) GT

func (d Decimal) GT(d2 Decimal) bool

GT returns true if d > d2

func (Decimal) GTE

func (d Decimal) GTE(d2 Decimal) bool

GTE returns true if d >= d2

func (Decimal) IsNegative

func (d Decimal) IsNegative() bool

IsNegative returns true if d < 0

func (Decimal) IsPositive

func (d Decimal) IsPositive() bool

IsPositive returns true if d > 0

func (Decimal) IsZero

func (d Decimal) IsZero() bool

IsZero returns true if d == 0

func (Decimal) LT

func (d Decimal) LT(d2 Decimal) bool

LT returns true if d < d2

func (Decimal) LTE

func (d Decimal) LTE(d2 Decimal) bool

LTE returns true if d <= d2

func (Decimal) MarshalJSON added in v0.0.7

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

MarshalJSON implements json.Marshaler by serializing the decimal as a string.

func (Decimal) Max

func (d Decimal) Max(d2 Decimal) Decimal

Max returns larger of d and d2

func (Decimal) Min

func (d Decimal) Min(d2 Decimal) Decimal

Min returns smaller of d and d2

func (Decimal) Mul

func (d Decimal) Mul(d2 Decimal) Decimal

Mul returns d * d2

func (Decimal) Neg

func (d Decimal) Neg() Decimal

Neg returns -d

func (Decimal) Pow

func (d Decimal) Pow(y int) Decimal

Pow returns d^y where y is an integer

func (Decimal) PowFloat

func (d Decimal) PowFloat(y float64) Decimal

PowFloat returns d^y where y is a float64 using math.Pow

func (Decimal) Round

func (d Decimal) Round() Decimal

Round returns d rounded to the nearest integer, with ties rounding away from zero

func (Decimal) Sign

func (d Decimal) Sign() int

Sign returns -1 if d < 0, 0 if d == 0, +1 if d > 0

func (Decimal) Sqrt

func (d Decimal) Sqrt() Decimal

Sqrt returns square root of d

func (Decimal) String

func (d Decimal) String() string

String returns string representation of d

func (Decimal) Sub

func (d Decimal) Sub(d2 Decimal) Decimal

Sub returns d - d2

func (Decimal) Truncate

func (d Decimal) Truncate() Decimal

Truncate returns the integer part of d, dropping any fractional part

func (*Decimal) UnmarshalJSON added in v0.0.7

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

UnmarshalJSON implements json.Unmarshaler by parsing a string or number.

func (Decimal) Zero

func (d Decimal) Zero() bool

Zero returns true if d == 0

Jump to

Keyboard shortcuts

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