money

package
v1.15.0 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2024 License: MIT Imports: 14 Imported by: 3

Documentation

Index

Constants

View Source
const (
	AmountAsStringFormat = `^(?:-)?\d+(\.\d+)?$`
)
View Source
const NanoDecimals = 9

Variables

View Source
var (
	ErrInvalidJSONUnmarshal  = errors.New("invalid json unmarshal")
	ErrorInvalidAmountString = errors.New("invalid string amount")
	ErrorInvalidAmountFloat  = errors.New("invalid float amount")
	ErrorInvalidCurrency     = errors.New("invalid currency")
	ErrorMissingAmount       = errors.New("missing amount")
	ErrorMissingCurrency     = errors.New("missing currency")
)
View Source
var AmountAsStringRegexp = regexp.MustCompile(AmountAsStringFormat)
View Source
var ErrCurrencyMismatch = fmt.Errorf("currencies don't match")
View Source
var ErrInvalidBSONUnmarshal = errors.New("invalid bson unmarshal")

Functions

func EqualAmount

func EqualAmount(t assert.TestingT, expected Money, actual Money)

func IsValidAmount added in v1.9.0

func IsValidAmount(value string) error

IsValidAmount returns no error if the amount is valid (positive or negative), an error if it is not.

Types

type CommonTypeMoney added in v1.1.3

type CommonTypeMoney interface {
	GetCurrencyCode() string
	GetUnits() int64
	GetNanos() int32
}

CommonTypeMoney allows to use a Google Common Type Money without creating a dependency on that package.

type Money

type Money struct {
	// contains filtered or unexported fields
}

func FromCommonType added in v1.1.3

func FromCommonType(cm CommonTypeMoney) Money

func FromFloat64

func FromFloat64(amount float64, currencyCode string) Money

func MustAdd

func MustAdd(a, b Money) Money

MustAdd panics if the two currencies are not the same currency

func MustParse

func MustParse(amount string, currencyCode string) Money

func NewFromInt

func NewFromInt(amount int64, currencyCode string) Money

func Parse

func Parse(amount string, currencyCode string) (Money, error)

func Zero

func Zero(currencyCode string) Money

func (Money) Add

func (a Money) Add(b Money) Money

Add sums the values including Zero

func (Money) Amount

func (a Money) Amount() string

Amount returns the amount as a string

func (Money) AsUnitsAndNanos added in v1.1.3

func (m Money) AsUnitsAndNanos() (int64, int32)

func (Money) By added in v1.14.0

func (a Money) By(multiplier float64) Money

By multiplies money by a floating number and returns result. It does not round the result.

func (Money) CheckSameCurrency

func (a Money) CheckSameCurrency(other Money) error

CheckSameCurrency returns an error if the other money is not the same currency

func (Money) Cmp

func (a Money) Cmp(b Money) int

Cmp compares two Money values. Returns -1 if a < b, 0 if a == b and 1 if a > b Panics if currencies are not the same

func (Money) CurrencyCode

func (a Money) CurrencyCode() string

CurrencyCode returns currency code of the Money

func (Money) Decimals added in v1.1.3

func (m Money) Decimals() int

func (Money) Div

func (a Money) Div(divider int64) Money

Div divides money and returns result without rounding

func (Money) Equal

func (a Money) Equal(another Money) bool

Equal compares two Money values. Returns true if a == b and false otherwise If values are zero, and at most one currency is specified, returns true

func (Money) GetCurrencyCode added in v1.2.0

func (a Money) GetCurrencyCode() string

GetCurrencyCode required for Money to implement CommonTypeMoney

func (Money) GetNanos added in v1.1.3

func (m Money) GetNanos() int32

func (Money) GetUnits added in v1.1.3

func (m Money) GetUnits() int64

func (Money) GoString

func (a Money) GoString() string

GoString implements fmt.GoStringer

func (Money) IsEmpty added in v1.8.2

func (a Money) IsEmpty() bool

IsEmpty returns true if the amount is zero and the currency is nil

func (Money) IsEqual added in v1.11.1

func (a Money) IsEqual(another Money) bool

IsEqual compares two Money values. Returns true if a == b and false otherwise If values are zero, and at most one currency is specified, returns true This is a synonym for Equals

func (Money) IsGreaterThan

func (a Money) IsGreaterThan(other Money) bool

IsGreaterThan returns true if the amount is greater than the other amount

func (Money) IsGreaterThanOrEqual added in v1.4.1

func (a Money) IsGreaterThanOrEqual(other Money) bool

IsGreaterThanOrEqual returns true if the amount is greater than or equal to the other amount

func (Money) IsLessThan added in v1.5.0

func (a Money) IsLessThan(amount Money) bool

IsLessThan is an alias for IsLessThan

func (Money) IsLessThanEqual

func (a Money) IsLessThanEqual(amount Money) bool

IsLessThanEqual is an alias for IsLessThanOrEqual Deprecated: Use IsLessThanOrEqual instead

func (Money) IsLessThanOrEqual added in v1.4.1

func (a Money) IsLessThanOrEqual(amount Money) bool

IsLessThanOrEqual returns true if the amount is less than or equal to the other amount

func (Money) IsNegative

func (a Money) IsNegative() bool

IsNegative returns true if the amount is less than zero

func (Money) IsNotEqual added in v1.11.1

func (a Money) IsNotEqual(another Money) bool

IsNotEqual compares two Money values. Returns true if a != b and false otherwise If values are zero, and both currencies are specified, and they are different, returns true

func (Money) IsPositive

func (a Money) IsPositive() bool

IsPositive returns true if the amount is greater than zero

func (Money) IsZero

func (a Money) IsZero() bool

IsZero returns true if the amount is zero

func (Money) LessThan

func (a Money) LessThan(amount Money) bool

LessThan is an alias for IsLessThan Deprecated: Use IsLessThan instead

func (Money) MarshalBSON

func (a Money) MarshalBSON() ([]byte, error)

MarshalBSON is implementation of bson.Marshaller

func (Money) MarshalJSON

func (a Money) MarshalJSON() ([]byte, error)

MarshalJSON is implementation of json.Marshaller

func (Money) Max added in v1.4.2

func (a Money) Max(other Money) Money

Max returns the larger of two Money values.

func (Money) Min

func (a Money) Min(other Money) Money

Min returns the smaller of two Money values.

func (Money) Mul

func (a Money) Mul(multiplier int64) Money

Mul multiplies money and returns result

func (Money) Negated

func (a Money) Negated() Money

Negated returns the negated value of the money

func (Money) Number

func (a Money) Number() float64

Number returns the amount as a float64

func (Money) RoundedBy added in v1.15.0

func (a Money) RoundedBy(multiplier float64) Money

RoundedBy multiplies money by a floating number and returns result. It does round the result.

func (Money) RoundedDiv

func (a Money) RoundedDiv(divider int64) Money

RoundedDiv divides money and rounds result using HalfEvenRounding

func (Money) Same added in v1.7.0

func (a Money) Same(another Money) bool

Same compares two Money values. Returns true if a == b and false otherwise. If values are zero, both currency should be equal or both nil.

func (Money) SameCurrency added in v1.3.0

func (m Money) SameCurrency(om Money) bool

SameCurrency check if given Money is equals by currency.

func (Money) Sign

func (a Money) Sign() int

Sign returns:

 1 if the amount is positive
 0 if the amount is zero
-1 if the amount is negative

func (Money) StepToZero added in v1.4.2

func (a Money) StepToZero() Money

StepToZero returns zero if the amount is negative, otherwise returns the amount. It corresponds to the Step function.

func (Money) String

func (a Money) String() string

String implements fmt.Stringer

func (Money) Sub

func (a Money) Sub(b Money) Money

Sub subtracts the values including Zero

func (Money) TryAdd

func (a Money) TryAdd(b Money) (Money, error)

TryAdd sums the values including Zero Returns error if currencies are not the same

func (Money) TryCmp

func (a Money) TryCmp(b Money) (int, error)

TryCmp compares two Money values. Returns -1 if a < b, 0 if a == b and 1 if a > b Returns error if currencies are not the same

func (Money) TryEqual

func (a Money) TryEqual(another Money) (bool, error)

TryEqual compares two Money values. Returns true if a == b and false otherwise Returns error if currencies are not the same Deprecated: Use TryEquals instead

func (Money) TryEquals added in v1.11.1

func (a Money) TryEquals(another Money) (bool, error)

TryEquals compares two Money values. Returns true if a == b and false otherwise Returns error if currencies are not the same

func (Money) TrySub

func (a Money) TrySub(b Money) (Money, error)

TrySub subtracts the values including Zero Returns error if currencies are not the same

func (*Money) UnmarshalBSON

func (a *Money) UnmarshalBSON(b []byte) error

UnmarshalBSON is implementation of json.Unmarshaller

func (*Money) UnmarshalJSON

func (a *Money) UnmarshalJSON(b []byte) error

UnmarshalJSON is implementation of json.Unmarshaller

func (Money) Zero added in v1.6.0

func (m Money) Zero() Money

Zero returns the zero-ed value of the money

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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