money

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2022 License: MIT Imports: 3 Imported by: 0

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

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

func MustNewAmount(amount *Amount, err error) *Amount

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

func NewAmountFromValueInt(
	v value.Int,
	decimals uint,
	currencyCode string,
) (*Amount, error)

NewAmountFromValueInt creates a new money amount from a *Int value. The value must be not nil.

func (Amount) CurrencyCode

func (a Amount) CurrencyCode() string

CurrencyCode returns the shorthand for the Currency Code of the Amount.

func (Amount) Decimals

func (a Amount) Decimals() uint

Decimals returns the number of decimals for the amount.

func (Amount) ToUnits

func (a Amount) ToUnits() *big.Float

ToUnits divides a.value / 10^decimals and returns a new big float containing the result.

func (Amount) ToUnitsString

func (a Amount) ToUnitsString(prec uint8) string

ToUnitsString divides a.value / 10^decimals and returns a new string formatted to the given precision prec.

func (Amount) Value

func (a Amount) Value() value.Int

Value returns the amount value in the *big.Int form.

type GRPCMessageAmountString

type GRPCMessageAmountString interface {
	GetAmount() string
	GetDecimals() uint32
	GetCurrencyCode() string
}

GRPCMessageAmountString defines an interface that is implemented by GRPC messages carrying an amount.

Jump to

Keyboard shortcuts

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