Documentation ¶
Index ¶
- Constants
- Variables
- func EqualAmount(t assert.TestingT, expected Money, actual Money)
- func IsValidAmount(value string) error
- type CommonTypeMoney
- type Money
- func FromCommonType(cm CommonTypeMoney) Money
- func FromFloat64(amount float64, currencyCode string) Money
- func MustAdd(a, b Money) Money
- func MustParse(amount string, currencyCode string) Money
- func NewFromInt(amount int64, currencyCode string) Money
- func Parse(amount string, currencyCode string) (Money, error)
- func Zero(currencyCode string) Money
- func (a Money) Add(b Money) Money
- func (a Money) Amount() string
- func (m Money) AsUnitsAndNanos() (int64, int32)
- func (a Money) By(multiplier float64) Money
- func (a Money) CheckSameCurrency(other Money) error
- func (a Money) Cmp(b Money) int
- func (a Money) CurrencyCode() string
- func (m Money) Decimals() int
- func (a Money) Div(divider int64) Money
- func (a Money) Equal(another Money) bool
- func (a Money) GetCurrencyCode() string
- func (m Money) GetNanos() int32
- func (m Money) GetUnits() int64
- func (a Money) GoString() string
- func (a Money) IsEmpty() bool
- func (a Money) IsEqual(another Money) bool
- func (a Money) IsGreaterThan(other Money) bool
- func (a Money) IsGreaterThanOrEqual(other Money) bool
- func (a Money) IsLessThan(amount Money) bool
- func (a Money) IsLessThanEqual(amount Money) bool
- func (a Money) IsLessThanOrEqual(amount Money) bool
- func (a Money) IsNegative() bool
- func (a Money) IsNotEqual(another Money) bool
- func (a Money) IsPositive() bool
- func (a Money) IsZero() bool
- func (a Money) LessThan(amount Money) bool
- func (a Money) MarshalBSON() ([]byte, error)
- func (a Money) MarshalJSON() ([]byte, error)
- func (a Money) Max(other Money) Money
- func (a Money) Min(other Money) Money
- func (a Money) Mul(multiplier int64) Money
- func (a Money) Negated() Money
- func (a Money) Number() float64
- func (a Money) RoundedBy(multiplier float64) Money
- func (a Money) RoundedDiv(divider int64) Money
- func (a Money) Same(another Money) bool
- func (m Money) SameCurrency(om Money) bool
- func (a Money) Sign() int
- func (a Money) StepToZero() Money
- func (a Money) String() string
- func (a Money) Sub(b Money) Money
- func (a Money) TryAdd(b Money) (Money, error)
- func (a Money) TryCmp(b Money) (int, error)
- func (a Money) TryEqual(another Money) (bool, error)
- func (a Money) TryEquals(another Money) (bool, error)
- func (a Money) TrySub(b Money) (Money, error)
- func (a *Money) UnmarshalBSON(b []byte) error
- func (a *Money) UnmarshalJSON(b []byte) error
- func (m Money) Zero() Money
Constants ¶
const (
AmountAsStringFormat = `^(?:-)?\d+(\.\d+)?$`
)
const NanoDecimals = 9
Variables ¶
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") )
var AmountAsStringRegexp = regexp.MustCompile(AmountAsStringFormat)
var ErrCurrencyMismatch = fmt.Errorf("currencies don't match")
var ErrInvalidBSONUnmarshal = errors.New("invalid bson unmarshal")
Functions ¶
func IsValidAmount ¶ added in v1.9.0
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
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 NewFromInt ¶
func (Money) AsUnitsAndNanos ¶ added in v1.1.3
func (Money) By ¶ added in v1.14.0
By multiplies money by a floating number and returns result. It does not round the result.
func (Money) CheckSameCurrency ¶
CheckSameCurrency returns an error if the other money is not the same currency
func (Money) Cmp ¶
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 ¶
CurrencyCode returns currency code of the Money
func (Money) Equal ¶
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
GetCurrencyCode required for Money to implement CommonTypeMoney
func (Money) IsEmpty ¶ added in v1.8.2
IsEmpty returns true if the amount is zero and the currency is nil
func (Money) IsEqual ¶ added in v1.11.1
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 ¶
IsGreaterThan returns true if the amount is greater than the other amount
func (Money) IsGreaterThanOrEqual ¶ added in v1.4.1
IsGreaterThanOrEqual returns true if the amount is greater than or equal to the other amount
func (Money) IsLessThan ¶ added in v1.5.0
IsLessThan is an alias for IsLessThan
func (Money) IsLessThanEqual ¶
IsLessThanEqual is an alias for IsLessThanOrEqual Deprecated: Use IsLessThanOrEqual instead
func (Money) IsLessThanOrEqual ¶ added in v1.4.1
IsLessThanOrEqual returns true if the amount is less than or equal to the other amount
func (Money) IsNegative ¶
IsNegative returns true if the amount is less than zero
func (Money) IsNotEqual ¶ added in v1.11.1
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 ¶
IsPositive returns true if the amount is greater than zero
func (Money) MarshalBSON ¶
MarshalBSON is implementation of bson.Marshaller
func (Money) MarshalJSON ¶
MarshalJSON is implementation of json.Marshaller
func (Money) RoundedBy ¶ added in v1.15.0
RoundedBy multiplies money by a floating number and returns result. It does round the result.
func (Money) RoundedDiv ¶
RoundedDiv divides money and rounds result using HalfEvenRounding
func (Money) Same ¶ added in v1.7.0
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
SameCurrency check if given Money is equals by currency.
func (Money) Sign ¶
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
StepToZero returns zero if the amount is negative, otherwise returns the amount. It corresponds to the Step function.
func (Money) TryAdd ¶
TryAdd sums the values including Zero Returns error if currencies are not the same
func (Money) TryCmp ¶
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 ¶
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
TryEquals compares two Money values. Returns true if a == b and false otherwise Returns error if currencies are not the same
func (Money) TrySub ¶
TrySub subtracts the values including Zero Returns error if currencies are not the same
func (*Money) UnmarshalBSON ¶
UnmarshalBSON is implementation of json.Unmarshaller
func (*Money) UnmarshalJSON ¶
UnmarshalJSON is implementation of json.Unmarshaller