Documentation ¶
Overview ¶
Package money implements an Amount type used to represent a monetary amount defined by the following properties:
- value, in the lowest denominator form, eg. cents for USD.
- decimals, the number of the digits after the decimals point, eg. 2 for USD.
- currency code, the shorthand for the currency, eg. USD for United States Dollar.
The package is placed under the ./value package as it imports from it.
Index ¶
- type Amount
- func MustNewAmount(amount *Amount, err error) *Amount
- func NewAmountFromBytesValue(valueBytes []byte, decimals uint, currencyCode string) (*Amount, error)
- func NewAmountFromGRPCMessageAmountString(m GRPCMessageAmountString) (*Amount, error)
- func NewAmountFromStringValue(valueStr string, decimals uint, currencyCode string) (*Amount, error)
- func NewAmountFromUnitStringAmount(unitValueStr string, decimals uint, currencyCode string) (*Amount, error)
- func NewAmountFromValueInt(v value.Int, decimals uint, currencyCode string) (*Amount, error)
- type GRPCMessageAmountString
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Amount ¶
type Amount struct {
// contains filtered or unexported fields
}
Amount represents a type storing information about a monetary amount.
! The value is stored in its smallest denomination of the currency. Example: for dollars the amount is stored in cents: for 97.23 dollars, the value is 9723.
Decimals represent the supported number of digits, after the decimals point. Example: - dollars decimals = 2 (smallest denomination: cents) - bitcoin decimals = 18 (smallest denomination: satoshi) - ethereum decimals = 18 (smallest denomination: wei).
func MustNewAmount ¶
MustNewAmount returns Amount if err is nil and panics otherwise.
func NewAmountFromBytesValue ¶
func NewAmountFromBytesValue( valueBytes []byte, decimals uint, currencyCode string, ) (*Amount, error)
NewAmountFromBytesValue creates a new amount from a []byte value. The value must be not nil.
func NewAmountFromGRPCMessageAmountString ¶
func NewAmountFromGRPCMessageAmountString( m GRPCMessageAmountString, ) (*Amount, error)
NewAmountFromGRPCMessageAmountString creates a new Amount from an interface expected to be implemented by a GRPC message.
func NewAmountFromStringValue ¶
func NewAmountFromStringValue( valueStr string, decimals uint, currencyCode string, ) (*Amount, error)
NewAmountFromStringValue creates a new amount from a string value. The value must be not empty. The value must be a valid int.
func NewAmountFromUnitStringAmount ¶
func NewAmountFromUnitStringAmount( unitValueStr string, decimals uint, currencyCode string, ) (*Amount, error)
NewAmountFromUnitStringAmount creates new Amount entity from a value that is in its largest denomination. The Amount value is calculated by multiplying unitValueStr * 10^decimals.
func NewAmountFromValueInt ¶
NewAmountFromValueInt creates a new money amount from a *Int value. The value must be not nil.
func (Amount) CurrencyCode ¶
CurrencyCode returns the shorthand for the Currency Code of the Amount.
func (Amount) ToUnits ¶
ToUnits divides a.value / 10^decimals and returns a new big float containing the result.
func (Amount) ToUnitsString ¶
ToUnitsString divides a.value / 10^decimals and returns a new string formatted to the given precision prec.
type GRPCMessageAmountString ¶
type GRPCMessageAmountString interface { GetAmount() string GetDecimals() uint32 GetCurrencyCode() string }
GRPCMessageAmountString defines an interface that is implemented by GRPC messages carrying an amount.