Documentation ¶
Overview ¶
Package protomoney provides basic, low-level mathematical and comparison operations for the "well-known" protocol buffers Money type without conversion to another data type.
The functions in this package treat a *money.Money value as immutable. Allocations are avoided as best possible, and minimal validation is performed.
Index ¶
- func Abs(m *money.Money) *money.Money
- func Add(a, b *money.Money) *money.Money
- func Cmp(a, b *money.Money) (c int)
- func EqualTo(a, b *money.Money) bool
- func Fmt(m *money.Money) fmt.Formatter
- func GreaterThan(a, b *money.Money) bool
- func GreaterThanOrEqualTo(a, b *money.Money) bool
- func IdenticalTo(a, b *money.Money) bool
- func IsNegative(m *money.Money) bool
- func IsPositive(m *money.Money) bool
- func IsZero(m *money.Money) bool
- func LessThan(a, b *money.Money) bool
- func LessThanOrEqualTo(a, b *money.Money) bool
- func LexicallyLessThan(a, b *money.Money) bool
- func Max(amounts ...*money.Money) *money.Money
- func Min(amounts ...*money.Money) *money.Money
- func Neg(m *money.Money) *money.Money
- func Normalize(m *money.Money) (*money.Money, error)
- func Sub(a, b *money.Money) *money.Money
- func Sum(amounts ...*money.Money) *money.Money
- func Validate(m *money.Money) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Abs ¶
Abs returns the absolute value of this amount.
That is, if m is negative, it returns its inverse (a positive magnitude), otherwise it returns m unchanged.
func Cmp ¶
Cmp compares a to b and returns a C-style comparison result.
It panics if a and b do not use the same currency.
If a < b then c is negative. If a > b then c is positive. Otherwise; a == b and c is zero.
func EqualTo ¶
Equal returns true if a and b have the same magnitude.
It panics if a and b do not use the same currency.
To check equality between two amounts that may have differing currencies, use IdenticalTo() instead.
func Fmt ¶ added in v0.1.1
Fmt wraps a money value in a formatter allows it to be formatted using standard fmt.Printf() verbs.
Example ¶
package main import ( "fmt" . "github.com/dogmatiq/dosh/protomoney" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "google.golang.org/genproto/googleapis/type/money" ) func main() { m := &money.Money{ CurrencyCode: "XYZ", Units: 10, Nanos: 129000000, } fmt.Printf("%.2f\n", Fmt(m)) } var _ = Describe("func Fmt()", func() { It("returns a formatted representation of the amount", func() { m := &money.Money{ CurrencyCode: "XYZ", Units: 10, Nanos: 129000000, } s := fmt.Sprintf("%0.2f", Fmt(m)) Expect(s).To(Equal("XYZ 10.13")) }) It("returns a descriptive string if used with an unsupported verb", func() { m := &money.Money{ CurrencyCode: "XYZ", Units: 10, Nanos: 129000000, } s := fmt.Sprintf("%d", Fmt(m)) Expect(s).To(Equal("%!d(*money.Money=" + m.String() + ")")) }) })
Output: XYZ 10.13
func GreaterThan ¶
GreaterThan returns true if a > b.
It panics if a and b do not use the same currency.
func GreaterThanOrEqualTo ¶
GreaterThanOrEqualTo returns true if a >= b.
It panics if a and b do not use the same currency.
func IdenticalTo ¶
Identical returns true if a and b use the same currency and have the same magnitude.
For general comparisons that are expected to be in the same currency, use EqualTo() instead.
func IsNegative ¶
IsNegative returns true if m has a negative magnitude.
func IsPositive ¶
IsPositive returns true if m has a positive magnitude.
func LessThanOrEqualTo ¶
LessThanOrEqualTo returns true if a <= b.
It panics if a and b do not use the same currency.
func LexicallyLessThan ¶
LexicallyLessThan returns true if a should appear before b in a sorted list.
There is no requirement that a and b use the same currency.
func Max ¶
Max returns the largest of the given amounts.
It panics if amounts is empty, or if the amounts do not use the same currency.
func Min ¶
Min returns the smallest of the given amounts.
It panics if amounts is empty, or if the amounts do not use the same currency.
func Normalize ¶
Normalize validates m, and returns its normalized version if it is valid.
Within a normalized value the nanos component is guaranteed to be less than one whole unit.
m itself is never mutated. If it is already normalized it is returned unchanged; otherwise a normalized clone is returned.
Types ¶
This section is empty.