money

package
v0.0.0-...-ec86544 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidValue        = errors.New("one of the specified money values is invalid")
	ErrMismatchingCurrency = errors.New("mismatching currency codes")
)

Functions

func AreEquals

func AreEquals(l, r T) bool

AreEquals returns true if values l and r are the equal, including the currency. This does not check validity of the provided values.

func AreSameCurrency

func AreSameCurrency(l, r T) bool

AreSameCurrency returns true if values l and r have a currency code and they are the same values.

func IsNegative

func IsNegative(m T) bool

IsNegative returns true if the specified money value is valid and is negative.

func IsPositive

func IsPositive(m T) bool

IsPositive returns true if the specified money value is valid and is positive.

func IsValid

func IsValid(m T) bool

IsValid checks if specified value has a valid units/nanos signs and ranges.

func IsZero

func IsZero(m T) bool

IsZero returns true if the specified money value is equal to zero.

Types

type T

type T struct {
	weaver.AutoMarshal

	// The 3-letter currency code defined in ISO 4217.
	CurrencyCode string `json:"currencyCode"`

	// The whole units of the amount.
	// For example if `currencyCode` is `"USD"`, then 1 unit is one US dollar.
	Units int64 `json:"units"`

	// Number of nano (10^-9) units of the amount.
	// The value must be between -999,999,999 and +999,999,999 inclusive.
	// If `units` is positive, `nanos` must be positive or zero.
	// If `units` is zero, `nanos` can be positive, zero, or negative.
	// If `units` is negative, `nanos` must be negative or zero.
	// For example $-1.75 is represented as `units`=-1 and `nanos`=-750,000,000.
	Nanos int32 `json:"nanos"`
}

T represents an amount of money along with the currency type.

func MultiplySlow

func MultiplySlow(m T, n uint32) T

MultiplySlow is a slow multiplication operation done through adding the value to itself n-1 times.

func Must

func Must(v T, err error) T

Must panics if the given error is not nil. This can be used with other functions like: "m := Must(Sum(a,b))".

func Negate

func Negate(m T) T

Negate returns the same amount with the sign negated.

func Sum

func Sum(l, r T) (T, error)

Sum adds two values. Returns an error if one of the values are invalid or currency codes are not matching (unless currency code is unspecified for both).

func (*T) WeaverMarshal

func (x *T) WeaverMarshal(enc *codegen.Encoder)

func (*T) WeaverUnmarshal

func (x *T) WeaverUnmarshal(dec *codegen.Decoder)

Jump to

Keyboard shortcuts

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