protomoney

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2022 License: MIT Imports: 4 Imported by: 0

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs(m *money.Money) *money.Money

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 Add

func Add(a, b *money.Money) *money.Money

Add returns a + b.

It panics if a and b do not use the same currency.

func Cmp

func Cmp(a, b *money.Money) (c int)

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

func EqualTo(a, b *money.Money) bool

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

func Fmt(m *money.Money) fmt.Formatter

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

func GreaterThan(a, b *money.Money) bool

GreaterThan returns true if a > b.

It panics if a and b do not use the same currency.

func GreaterThanOrEqualTo

func GreaterThanOrEqualTo(a, b *money.Money) bool

GreaterThanOrEqualTo returns true if a >= b.

It panics if a and b do not use the same currency.

func IdenticalTo

func IdenticalTo(a, b *money.Money) bool

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

func IsNegative(m *money.Money) bool

IsNegative returns true if m has a negative magnitude.

func IsPositive

func IsPositive(m *money.Money) bool

IsPositive returns true if m has a positive magnitude.

func IsZero

func IsZero(m *money.Money) bool

IsZero returns true if m has a magnitude of zero.

func LessThan

func LessThan(a, b *money.Money) bool

LessThan returns true if a < b.

It panics if a and b do not use the same currency.

func LessThanOrEqualTo

func LessThanOrEqualTo(a, b *money.Money) bool

LessThanOrEqualTo returns true if a <= b.

It panics if a and b do not use the same currency.

func LexicallyLessThan

func LexicallyLessThan(a, b *money.Money) bool

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

func Max(amounts ...*money.Money) *money.Money

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

func Min(amounts ...*money.Money) *money.Money

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 Neg

func Neg(m *money.Money) *money.Money

Neg returns -m.

func Normalize

func Normalize(m *money.Money) (*money.Money, error)

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.

func Sub

func Sub(a, b *money.Money) *money.Money

Sub returns a - b.

It panics if a and b do not use the same currency.

func Sum

func Sum(amounts ...*money.Money) *money.Money

Sum returns the sum of the given amounts.

It panics if amounts is empty, or if the amounts do not use the same currency.

func Validate

func Validate(m *money.Money) error

Validate returns an error if m is invalid.

Types

This section is empty.

Jump to

Keyboard shortcuts

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