Documentation ¶
Index ¶
- func RoundDown(f float64) int64
- func RoundHalfDown(f float64) int64
- func RoundHalfToEven(f float64) int64
- func RoundHalfUp(f float64) int64
- func RoundUp(f float64) int64
- type Money
- func MoneyEUR[T constraints.Integer](value T) (Money, error)
- func MoneyFromFloat[T constraints.Float](currIsoCode string, value T, f roundFunc) (Money, error)
- func MoneyFromString(currIsoCode string, str string, f roundFunc) (Money, error)
- func MoneyFromSubunits[T constraints.Integer](currIsoCode string, value T, f roundFunc) (Money, error)
- func MoneyGBP[T constraints.Integer](value T) (Money, error)
- func MoneyUSD[T constraints.Integer](value T) (Money, error)
- func (m Money) Abs() Money
- func (m Money) Add(v Money) Money
- func (m Money) Allocate(ratios ...int64) []Money
- func (m Money) Clone(value int64) Money
- func (m Money) Div(f float64) Money
- func (m Money) Eq(v Money) bool
- func (m Money) FlipSign() Money
- func (m Money) Gt(v Money) bool
- func (m Money) Gte(v Money) bool
- func (m Money) IsNeg() bool
- func (m Money) IsPos() bool
- func (m Money) IsZero() bool
- func (m Money) IsoCode() string
- func (m Money) Lt(v Money) bool
- func (m Money) Lte(v Money) bool
- func (m Money) MarshalJSON() ([]byte, error)
- func (m Money) Mul(n int64) Money
- func (m Money) Neq(v Money) bool
- func (m Money) Split(n int64) []Money
- func (m Money) String() string
- func (m Money) StringNoSymbol() string
- func (m Money) Sub(v Money) Money
- func (m Money) Subunits() int64
- func (m Money) Units() int64
- func (m Money) Value() int64
- type Price
- func PriceFromFloat[T constraints.Float](currIsoCode string, gross T, f roundFunc) (Price, error)
- func PriceFromString(currIsoCode string, gross string, f roundFunc) (Price, error)
- func PriceFromSubunits[T constraints.Integer](currIsoCode string, gross T, f roundFunc) (Price, error)
- func PriceGBP[T constraints.Integer](gross T, vat float64) (Price, error)
- func (p Price) Add(v Price) Price
- func (p *Price) AddTax(m Money, desc string)
- func (p *Price) AddTaxPercent(percent float64, desc string)
- func (p Price) Gross() Money
- func (p *Price) IncludeTax(m Money, desc string)
- func (p *Price) IncludeTaxPercent(percent float64, desc string)
- func (p Price) IsoCode() string
- func (p Price) MarshalJSON() ([]byte, error)
- func (p Price) Mul(n int64) Price
- func (p Price) Net() Money
- func (p Price) String() string
- func (p Price) StringNoSymbol() string
- func (p Price) Tax() Money
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RoundHalfDown ¶
RoundHalfDown is a standard rounding function that rounds 0.5 and below down.
func RoundHalfToEven ¶
RoundHalfToEven is a standard rounding function that rounds 0.5 to the nearest even number. This is sometimes called bankers rounding.
func RoundHalfUp ¶
RoundHalfUp is a standard rounding function that rounds 0.5 and above up.
Types ¶
type Money ¶
type Money struct {
// contains filtered or unexported fields
}
Money is the main structure that holds a monetary value and how to format it as a string.
func MoneyEUR ¶
func MoneyEUR[T constraints.Integer](value T) (Money, error)
MoneyEUR is a helper function.
func MoneyFromFloat ¶ added in v1.2.2
func MoneyFromFloat[T constraints.Float](currIsoCode string, value T, f roundFunc) (Money, error)
MoneyFromFloat constructs a new money object from a floating point number. currIsoCode is an ISO 4217 currency code. value is monetary value expressed as a float. roundFunc is a function to be used for division operations.
func MoneyFromString ¶
MoneyFromString constructs a new money object from a string. Everything not contained within a number is stripped out before parsing. currIsoCode is an ISO 4217 currency code. str is monetary value expressed as a string. roundFunc is a function to be used for division operations.
func MoneyFromSubunits ¶
func MoneyFromSubunits[T constraints.Integer](currIsoCode string, value T, f roundFunc) (Money, error)
MoneyFromSubunits constructs a new money object from an integer. The integer used should represent the subunits of the currency. currIsoCode is an ISO 4217 currency code. value is monetary value in subunits. roundFunc is a function to be used for division operations.
func MoneyGBP ¶
func MoneyGBP[T constraints.Integer](value T) (Money, error)
MoneyGBP is a helper function.
func MoneyUSD ¶ added in v1.1.0
func MoneyUSD[T constraints.Integer](value T) (Money, error)
MoneyUSD is a helper function.
func (Money) Allocate ¶
Allocate returns a slice containing money objects split according to the passed ratios. The ratios are completely arbitrary and are calculated as percentages of the overall sum. This operation is lossless and will account for all remainders.
func (Money) Div ¶
Div is an arithmetic operator. This operation will perform rounding of the resulting value using the assigned rounding function. If you need to accurately divide a money object with lossless precision, use the Split or Allocate functions instead.
func (Money) FlipSign ¶
FlipSign flips the sign of the money object's value. Switching positive to negative and vice versa.
func (Money) MarshalJSON ¶
MarshalJSON is an implementation of json.Marshaller.
func (Money) Split ¶
Split returns a slice containing money objects split as evenly as possible by 'n' times. This operation is lossless and will account for all remainders.
func (Money) String ¶
String is an implementation of fmt.Stringer and returns the string formatted representation of the monetary value.
func (Money) StringNoSymbol ¶
StringNoSymbol returns the string formatted representation of the monetary value without a currency symbol.
type Price ¶
type Price struct {
// contains filtered or unexported fields
}
Price is a structure that holds a price and gives information about the amount of tax applied to that price.
func PriceFromFloat ¶ added in v1.2.2
func PriceFromFloat[T constraints.Float](currIsoCode string, gross T, f roundFunc) (Price, error)
PriceFromFloat constructs a new price object from a floating point number. currIsoCode is an ISO 4217 currency code. gross is monetary value expressed as a float. roundFunc is a function to be used for division operations.
func PriceFromString ¶
PriceFromString constructs a new price object from a string. Everything not contained within a number is stripped out before parsing. currIsoCode is an ISO 4217 currency code. gross is monetary value expressed as a string. roundFunc is a function to be used for division operations.
func PriceFromSubunits ¶
func PriceFromSubunits[T constraints.Integer](currIsoCode string, gross T, f roundFunc) (Price, error)
PriceFromSubunits constructs a new price object from an integer. currIsoCode is an ISO 4217 currency code. gross is monetary value in subunits. roundFunc is a function to be used for division operations.
func PriceGBP ¶
func PriceGBP[T constraints.Integer](gross T, vat float64) (Price, error)
PriceGBP is a helper function. gross is the gross monetary value in subunits. vat is a tax percentage that's included in the gross value.
func (*Price) AddTax ¶ added in v1.1.4
AddTax adds a tax to the price using a money value. This will literally add the money amount to the gross price.
func (*Price) AddTaxPercent ¶ added in v1.1.0
AddTaxPercentage adds a tax to the price using a percentage. This will literally add a percentage to the gross price.
func (*Price) IncludeTax ¶ added in v1.1.4
IncludeTax adds a tax to the price using a money value. This implies this tax is already included in the gross price.
func (*Price) IncludeTaxPercent ¶ added in v1.1.0
IncludeTaxPercent adds a tax to the price using a percentage. This implies this tax is already included in the gross price.
func (Price) MarshalJSON ¶
MarshalJSON is an implementation of json.Marshaller.
func (Price) Net ¶
Net returns the net monetary value of the price which is equal to the gross minus tax.
func (Price) String ¶
String is an implementation of fmt.Stringer and returns the string formatted representation of the price value.
func (Price) StringNoSymbol ¶
StringNoSymbol returns the string formatted representation of the price value without a currency symbol.