dinero

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNonDecimalCurrency = errors.New("non-decimal currency")

Functions

func HaveSameAmount

func HaveSameAmount[T any](dineros ...Dinero[T]) bool

func HaveSameCurrency

func HaveSameCurrency[T any](dineros ...Dinero[T]) bool

Types

type Dinero

type Dinero[T any] struct {
	Amount   T                    `json:"amount"`
	Currency currency.Currency[T] `json:"currency"`
	Scale    T                    `json:"scale"`
	// contains filtered or unexported fields
}

func Maximum

func Maximum[T any](dineros ...Dinero[T]) (Dinero[T], error)

Returns the greatest of the passed Dineros.

func Minimum

func Minimum[T any](dineros ...Dinero[T]) (Dinero[T], error)

Returns the lowest of the passed Dineros.

func NewBigDinero added in v1.0.0

func NewBigDinero(amount int64, currency currency.Currency[*big.Int]) Dinero[*big.Int]

func NewBigDineroWithScale added in v1.0.0

func NewBigDineroWithScale(amount int64, currency currency.Currency[*big.Int], scale int64) Dinero[*big.Int]

func NewDinero

func NewDinero(amount int, currency currency.Currency[int]) Dinero[int]

func NewDineroWithOptions

func NewDineroWithOptions[T any](
	amount T,
	currency currency.Currency[T],
	scale T,
	calculator calculator.Calculator[T],
) Dinero[T]

func NewDineroWithScale

func NewDineroWithScale(amount int, currency currency.Currency[int], scale int) Dinero[int]

func NormalizeScale

func NormalizeScale[T any](dineros ...Dinero[T]) []Dinero[T]

Normalize a set of Dinero objects to the highest scale of the set.

Normalizing to a higher scale means that the internal amount value increases by orders of magnitude. If you're using the default Dinero implementation (with the int calculator), be careful not to exceed the minimum and maximum safe integers.

func (Dinero[T]) Add

func (d Dinero[T]) Add(addend Dinero[T]) (Dinero[T], error)

Add addend to d and return a new Dinero.

You can only add objects that share the same currency. The function also normalizes objects to the same scale (the highest) before adding them up.

func (Dinero[T]) Allocate

func (d Dinero[T]) Allocate(ratios ...T) ([]Dinero[T], error)

Distribute the amount of a Dinero object across a list of ratios. To distribute with a ratio less than 1, use the AllocateScaled function.

Monetary values have indivisible units, meaning you can't always exactly split them. With allocate, you can split a monetary amount then distribute the remainder as evenly as possible. You can use percentage or ratio style for ratios: [25, 75] and [1, 3] do the same thing. You can also pass zero ratios (such as [0, 50, 50]). If there's a remainder to distribute, zero ratios are skipped and return a Dinero object with amount zero.

func (Dinero[T]) AllocateScaled

func (d Dinero[T]) AllocateScaled(ratios ...ScaledAmount[T]) ([]Dinero[T], error)

Distribute the amount of a Dinero object across a list of scaled ratios.

func (*Dinero[T]) Calculator added in v1.0.0

func (d *Dinero[T]) Calculator() calculator.Calculator[T]

Get the calculator or find the correct type if nil.

func (Dinero[T]) Compare

func (d Dinero[T]) Compare(comparator Dinero[T]) (calculator.CompareResult, error)

Compare the value of d relative to comparator. Returns one of LT, EQ, or GT depending on whether d is less than, equal to, or greater than comparator.

You can only compare objects that share the same currency. The function also normalizes objects to the same scale (the highest) before comparing them.

func (Dinero[T]) Convert

func (d Dinero[T]) Convert(currency currency.Currency[T], rates map[string]ScaledAmount[T]) (Dinero[T], error)

Convert a Dinero object from a currency to another.

func (Dinero[T]) Equal

func (d Dinero[T]) Equal(comparator Dinero[T]) bool

Check whether the value of a Dinero object is equal to another.

This function does same-value equality, determining whether two Dinero objects are functionally equivalent. It also normalizes objects to the same scale (the highest) before comparing them.

func (Dinero[T]) GreaterThan

func (d Dinero[T]) GreaterThan(dinero Dinero[T]) bool

Returns true if d is more than comparator. It will always return false if they have different currencies.

func (Dinero[T]) GreaterThanOrEqual

func (d Dinero[T]) GreaterThanOrEqual(dinero Dinero[T]) bool

Returns true if d is greater than or equal to comparator. It will always return false if they have different currencies.

func (Dinero[T]) HasSubUnits

func (d Dinero[T]) HasSubUnits() bool

func (Dinero[T]) IsNegative

func (d Dinero[T]) IsNegative() bool

Return true if d has a negative amount.

func (Dinero[T]) IsPositive

func (d Dinero[T]) IsPositive() bool

Return true if d has a positive amount.

func (Dinero[T]) IsZero

func (d Dinero[T]) IsZero() bool

Return true if d has a zero amount.

func (Dinero[T]) LessThan

func (d Dinero[T]) LessThan(dinero Dinero[T]) bool

Returns true if d is less than comparator. It will always return false if they have different currencies.

func (Dinero[T]) LessThanOrEqual

func (d Dinero[T]) LessThanOrEqual(dinero Dinero[T]) bool

Returns true if d is less than or equal to comparator. It will always return false if they have different currencies.

func (Dinero[T]) Multiply

func (d Dinero[T]) Multiply(multiplier T) Dinero[T]

Multiply the passed Dinero object. To multiply by a fraction, use MultiplyScaled.

func (Dinero[T]) MultiplyScaled

func (d Dinero[T]) MultiplyScaled(multiplier ScaledAmount[T]) (Dinero[T], error)

Multiply the passed Dinero object by a ScaledAmount. To multiply by 2.1, you would pass { Amount: 21, Scale: 1 }. When using scaled amounts, the function converts the returned objects to the safest scale.

func (Dinero[T]) Subtract

func (d Dinero[T]) Subtract(subtrahend Dinero[T]) (Dinero[T], error)

Subtract the passed Dinero object from d.

You can only subtract objects that share the same currency. The function also normalizes objects to the same scale (the highest) before subtracting them.

func (Dinero[T]) ToDecimal added in v1.0.0

func (d Dinero[T]) ToDecimal(options ...Option[T]) (string, error)

func (Dinero[T]) ToUnit

func (d Dinero[T]) ToUnit() ([]T, error)

func (Dinero[T]) TransformScale

func (d Dinero[T]) TransformScale(newScale T, divider divide.Divider[T]) (Dinero[T], error)

Transform a Dinero object to a new scale.

When transforming to a higher scale, the internal amount value increases by orders of magnitude. If you're using the default Dinero implementation (with the int calculator), be careful not to exceed the minimum and maximum safe integers.

When transforming to a smaller scale, the amount loses precision. By default, the function rounds down the amount when passing nil as the divider. You can specify how to round by passing a custom divide function.

For convenience, Dinero.go provides the following divide functions: up, down, halfUp, halfDown, halfOdd, halfEven (bankers rounding), halfTowardsZero, and halfAwayFromZero.

func (Dinero[T]) TrimScale

func (d Dinero[T]) TrimScale() (Dinero[T], error)

Trim a Dinero object's scale as much as possible, down to the currency exponent.

func (*Dinero[T]) WithCalculator added in v1.0.0

func (d *Dinero[T]) WithCalculator(calculator calculator.Calculator[T]) Dinero[T]

type Option added in v1.0.0

type Option[T any] func(*Options[T])

func WithTransformer added in v1.0.0

func WithTransformer[T any](t Transformer[T]) Option[T]

type Options added in v1.0.0

type Options[T any] struct {
	// contains filtered or unexported fields
}

type ScaledAmount

type ScaledAmount[T any] struct {
	Amount T `json:"amount"`
	Scale  T `json:"scale"`
}

Used to create decimal values without floats. 1.89 is expressed as { Amount: 189, Scale: 2 }.

func NewScaledAmount

func NewScaledAmount[T any](amount, scale T) ScaledAmount[T]

type Transformer added in v1.0.0

type Transformer[T any] func(value string, currency currency.Currency[T]) string

Jump to

Keyboard shortcuts

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