bigdecimal

package
v1.64.1 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

README

Bigdecimal

github.com/flanksource/commons/bigdecimal extends github.com/woodsbury/decimal128 with Commons arithmetic results, canonical formatting, quoted JSON, and currency-aware Money. See Decimal and Money for the shared API, examples, and serialization rules.

bigdecimal.Money also does arithmetic. Add and Sub take another Money in the same currency and keep whichever operand shows more decimal places. Mul, Quo and Neg take a plain Decimal, and the result stays in the Money's currency. Mixing currencies, using an uninitialized Money, or getting a non-finite result (such as dividing by zero) returns an error.

fee, _ := bigdecimal.NewMoney(bigdecimal.MoneyOptions{Amount: "12.5", Currency: "ZAR", DecimalPlaces: 2})
total, err := fee.Mul(bigdecimal.MustParse("3")) // ZAR 37.50

Documentation

Overview

Package bigdecimal extends decimal128 with canonical presentation and Money. Native arithmetic and rounding semantics are preserved, including special values.

Code generated by internal/gendecimal; DO NOT EDIT.

Index

Constants

View Source
const (
	ToNearestEven = upstream.ToNearestEven
	ToNearestAway = upstream.ToNearestAway
	ToZero        = upstream.ToZero
	AwayFromZero  = upstream.AwayFromZero
	ToNegativeInf = upstream.ToNegativeInf
	ToPositiveInf = upstream.ToPositiveInf
)

Variables

View Source
var One = FromInt64(1)
View Source
var Zero = Decimal{}

Functions

func Compare

func Compare(d Decimal, o Decimal) int

Types

type CmpResult

type CmpResult = upstream.CmpResult

type Decimal

type Decimal struct{ upstream.Decimal }

func Abs

func Abs(d Decimal) Decimal

func Cbrt

func Cbrt(d Decimal) Decimal

func Ceil

func Ceil(d Decimal) Decimal

func E

func E() Decimal

func Exp

func Exp(d Decimal) Decimal

func Exp2

func Exp2(d Decimal) Decimal

func Exp10

func Exp10(d Decimal) Decimal

func Expm1

func Expm1(d Decimal) Decimal

func Floor

func Floor(d Decimal) Decimal

func Frexp

func Frexp(d Decimal) (Decimal, int)

func FromFloat

func FromFloat(f *big.Float) Decimal

func FromFloat32

func FromFloat32(f float32) Decimal

func FromFloat64

func FromFloat64(f float64) Decimal

func FromInt

func FromInt(i *big.Int) Decimal

func FromInt32

func FromInt32(i int32) Decimal

func FromInt64

func FromInt64(i int64) Decimal

func FromRat

func FromRat(r *big.Rat) Decimal

func FromUint32

func FromUint32(i uint32) Decimal

func FromUint64

func FromUint64(i uint64) Decimal

func Inf

func Inf(sign int) Decimal

func Ldexp

func Ldexp(frac Decimal, exp int) Decimal

func Log

func Log(d Decimal) Decimal

func Log1p

func Log1p(d Decimal) Decimal

func Log2

func Log2(d Decimal) Decimal

func Log10

func Log10(d Decimal) Decimal

func Max

func Max(d Decimal, o Decimal) Decimal

func Min

func Min(d Decimal, o Decimal) Decimal

func MustParse

func MustParse(s string) Decimal

func NaN

func NaN() Decimal

func New

func New(sig int64, exp int) Decimal

func Parse

func Parse(s string) (Decimal, error)

func Phi

func Phi() Decimal

func Pi

func Pi() Decimal

func Round

func Round(d Decimal) Decimal

func Sqrt

func Sqrt(d Decimal) Decimal

func Trunc

func Trunc(d Decimal) Decimal

func (Decimal) Add

func (d Decimal) Add(o Decimal) Decimal

func (Decimal) AddWithMode

func (d Decimal) AddWithMode(o Decimal, mode RoundingMode) Decimal

func (Decimal) AppendText

func (d Decimal) AppendText(dst []byte) ([]byte, error)

func (Decimal) Canonical

func (d Decimal) Canonical() Decimal

func (Decimal) Ceil

func (d Decimal) Ceil(dp int) Decimal

func (Decimal) Cmp

func (d Decimal) Cmp(o Decimal) CmpResult

func (Decimal) CmpAbs

func (d Decimal) CmpAbs(o Decimal) CmpResult

func (Decimal) Compare

func (d Decimal) Compare(other Decimal) int

Compare returns -1, 0 or 1 as d is less than, equal to or greater than other, using the upstream total order: NaN is less than every other value and equal to NaN.

func (Decimal) Decompose

func (d Decimal) Decompose(buf []byte) (byte, bool, []byte, int32)

func (Decimal) Equal

func (d Decimal) Equal(o Decimal) bool

func (Decimal) Float

func (d Decimal) Float(f *big.Float) *big.Float

func (Decimal) Float32

func (d Decimal) Float32() float32

func (Decimal) Float64

func (d Decimal) Float64() float64

func (Decimal) Floor

func (d Decimal) Floor(dp int) Decimal

func (Decimal) Format

func (d Decimal) Format(state fmt.State, verb rune)

func (Decimal) Int

func (d Decimal) Int(i *big.Int) *big.Int

func (Decimal) Int32

func (d Decimal) Int32() (int32, bool)

func (Decimal) Int64

func (d Decimal) Int64() (int64, bool)

func (Decimal) IsInf

func (d Decimal) IsInf(sign int) bool

func (Decimal) IsNaN

func (d Decimal) IsNaN() bool

func (Decimal) IsZero

func (d Decimal) IsZero() bool

func (Decimal) MarshalJSON

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

func (Decimal) MarshalText

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

func (Decimal) Mul

func (d Decimal) Mul(o Decimal) Decimal

func (Decimal) MulWithMode

func (d Decimal) MulWithMode(o Decimal, mode RoundingMode) Decimal

func (Decimal) Neg

func (d Decimal) Neg() Decimal

func (Decimal) Payload

func (d Decimal) Payload() Payload

func (Decimal) Pow

func (d Decimal) Pow(o Decimal) Decimal

func (Decimal) PowWithMode

func (d Decimal) PowWithMode(o Decimal, mode RoundingMode) Decimal

func (Decimal) Quo

func (d Decimal) Quo(o Decimal) Decimal

func (Decimal) QuoRem

func (d Decimal) QuoRem(o Decimal) (Decimal, Decimal)

func (Decimal) QuoRemWithMode

func (d Decimal) QuoRemWithMode(o Decimal, mode RoundingMode) (Decimal, Decimal)

func (Decimal) QuoWithMode

func (d Decimal) QuoWithMode(o Decimal, mode RoundingMode) Decimal

func (Decimal) Rat

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

func (Decimal) Round

func (d Decimal) Round(dp int, mode RoundingMode) Decimal

func (Decimal) Sign

func (d Decimal) Sign() int

func (Decimal) Signbit

func (d Decimal) Signbit() bool

func (Decimal) String

func (d Decimal) String() string

func (Decimal) Sub

func (d Decimal) Sub(o Decimal) Decimal

func (Decimal) SubWithMode

func (d Decimal) SubWithMode(o Decimal, mode RoundingMode) Decimal

func (Decimal) Uint32

func (d Decimal) Uint32() (uint32, bool)

func (Decimal) Uint64

func (d Decimal) Uint64() (uint64, bool)

func (*Decimal) UnmarshalJSON

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

func (*Decimal) UnmarshalText

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

type Money

type Money struct{ money.Value[Decimal] }

func NewMoney

func NewMoney(opts MoneyOptions) (Money, error)

func (Money) Add

func (m Money) Add(other Money) (Money, error)

Add sums amounts in the same currency; the result keeps the wider display places.

func (Money) Cmp

func (m Money) Cmp(other Money) (int, error)

Cmp compares amounts in the same currency and rejects uninitialized values.

func (Money) Mul

func (m Money) Mul(factor Decimal) (Money, error)

Mul multiplies the amount by a plain factor, keeping the currency.

func (Money) Neg

func (m Money) Neg() (Money, error)

Neg returns the amount with its sign flipped.

func (Money) Quo

func (m Money) Quo(divisor Decimal) (Money, error)

Quo divides the amount by a plain divisor; a zero divisor is an error.

func (Money) Sub

func (m Money) Sub(other Money) (Money, error)

Sub subtracts an amount in the same currency; the result keeps the wider display places.

type MoneyOptions

type MoneyOptions = money.Options

type Payload

type Payload = upstream.Payload

type RoundingMode

type RoundingMode = upstream.RoundingMode

Jump to

Keyboard shortcuts

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