Documentation ¶
Index ¶
- Variables
- func HaveSameAmount[T any](dineros ...Dinero[T]) bool
- func HaveSameCurrency[T any](dineros ...Dinero[T]) bool
- type Dinero
- func Maximum[T any](dineros ...Dinero[T]) (Dinero[T], error)
- func Minimum[T any](dineros ...Dinero[T]) (Dinero[T], error)
- func NewBigDinero(amount int64, currency currency.Currency[*big.Int]) Dinero[*big.Int]
- func NewBigDineroWithScale(amount int64, currency currency.Currency[*big.Int], scale int64) Dinero[*big.Int]
- func NewDinero(amount int, currency currency.Currency[int]) Dinero[int]
- func NewDineroWithOptions[T any](amount T, currency currency.Currency[T], scale T, ...) Dinero[T]
- func NewDineroWithScale(amount int, currency currency.Currency[int], scale int) Dinero[int]
- func NormalizeScale[T any](dineros ...Dinero[T]) []Dinero[T]
- func (d Dinero[T]) Add(addend Dinero[T]) (Dinero[T], error)
- func (d Dinero[T]) Allocate(ratios ...T) ([]Dinero[T], error)
- func (d Dinero[T]) AllocateScaled(ratios ...ScaledAmount[T]) ([]Dinero[T], error)
- func (d *Dinero[T]) Calculator() calculator.Calculator[T]
- func (d Dinero[T]) Compare(comparator Dinero[T]) (calculator.CompareResult, error)
- func (d Dinero[T]) Convert(currency currency.Currency[T], rates map[string]ScaledAmount[T]) (Dinero[T], error)
- func (d Dinero[T]) Equal(comparator Dinero[T]) bool
- func (d Dinero[T]) GreaterThan(dinero Dinero[T]) bool
- func (d Dinero[T]) GreaterThanOrEqual(dinero Dinero[T]) bool
- func (d Dinero[T]) HasSubUnits() bool
- func (d Dinero[T]) IsNegative() bool
- func (d Dinero[T]) IsPositive() bool
- func (d Dinero[T]) IsZero() bool
- func (d Dinero[T]) LessThan(dinero Dinero[T]) bool
- func (d Dinero[T]) LessThanOrEqual(dinero Dinero[T]) bool
- func (d Dinero[T]) Multiply(multiplier T) Dinero[T]
- func (d Dinero[T]) MultiplyScaled(multiplier ScaledAmount[T]) (Dinero[T], error)
- func (d Dinero[T]) Subtract(subtrahend Dinero[T]) (Dinero[T], error)
- func (d Dinero[T]) ToDecimal(options ...Option[T]) (string, error)
- func (d Dinero[T]) ToUnit() ([]T, error)
- func (d Dinero[T]) TransformScale(newScale T, divider divide.Divider[T]) (Dinero[T], error)
- func (d Dinero[T]) TrimScale() (Dinero[T], error)
- func (d *Dinero[T]) WithCalculator(calculator calculator.Calculator[T]) Dinero[T]
- type Option
- type Options
- type ScaledAmount
- type Transformer
Constants ¶
This section is empty.
Variables ¶
var BigIntCalculator = calculator.NewCalculator[*big.Int](bigcalc.Calculator{})
var ErrNonDecimalCurrency = errors.New("non-decimal currency")
var IntCalculator = calculator.NewCalculator[int](integer.Calculator{})
Functions ¶
func HaveSameAmount ¶
func HaveSameCurrency ¶
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 NewBigDinero ¶ added in v1.0.0
func NewBigDineroWithScale ¶ added in v1.0.0
func NewDineroWithOptions ¶
func NewDineroWithOptions[T any]( amount T, currency currency.Currency[T], scale T, calculator calculator.Calculator[T], ) Dinero[T]
func NewDineroWithScale ¶
func NormalizeScale ¶
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 ¶
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 ¶
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 ¶
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 ¶
Returns true if d is more than comparator. It will always return false if they have different currencies.
func (Dinero[T]) GreaterThanOrEqual ¶
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 (Dinero[T]) IsNegative ¶
Return true if d has a negative amount.
func (Dinero[T]) IsPositive ¶
Return true if d has a positive amount.
func (Dinero[T]) LessThan ¶
Returns true if d is less than comparator. It will always return false if they have different currencies.
func (Dinero[T]) LessThanOrEqual ¶
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 ¶
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 ¶
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]) TransformScale ¶
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 ¶
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
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]
Source Files ¶
- add.go
- allocate.go
- compare.go
- convert.go
- dinero.go
- equal.go
- greater_than.go
- greater_than_or_equal.go
- has_sub_units.go
- have_same_amount.go
- have_same_currency.go
- is_negative.go
- is_positive.go
- is_zero.go
- less_than.go
- less_than_or_equal.go
- maximum.go
- minimum.go
- multiply.go
- normalize_scale.go
- options.go
- scaled_amount.go
- subtract.go
- to_decimal.go
- to_unit.go
- transform_scale.go
- trim_scale.go