mongo

package module
v1.2.3 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2023 License: MIT Imports: 8 Imported by: 0

README

mongo

A straightforward money library for Go


Overview

Mongo is a straightforward money library for Go that makes it easy to handle the usually bug prone arithmetic when dealing with money.

Documentation

https://pkg.golang.ir/github.com/nomad-software/mongo@master#section-documentation

Example 1

package main

import (
	"encoding/json"
	"fmt"
	"log"

	"github.com/nomad-software/mongo"
)

func main() {

	m, err := mongo.MoneyGBP(1055)

	if err != nil {
		log.Fatal("Error occured creating money")
	}

	fmt.Printf("Money: %s\n", m)

	shares := m.Split(3)
	fmt.Printf("Shares: %s\n", shares)

	shares = m.Allocate(1, 2, 3)
	fmt.Printf("Allocations: %s\n", shares)

	json, _ := json.Marshal(m)
	fmt.Println(string(json))
}
Output
Money: £10.55
Shares: [£3.52 £3.52 £3.51]
Allocations: [£1.76 £3.52 £5.27]
{"currency":"GBP","amount":"£10.55"}

Example 2

package main

import (
	"encoding/json"
	"fmt"
	"log"

	"github.com/nomad-software/mongo"
)

func main() {

	m, err := mongo.PriceGBP(1055, 17.5)

	if err != nil {
		log.Fatal("Error occured creating price")
	}

	fmt.Printf("Price: %s\n", m)

	json, _ := json.Marshal(m)
	fmt.Println(string(json))
}
Output
Price: £10.55
{"currency":"GBP","gross":"£10.55","net":"£8.98","tax":{"total":"£1.57","detail":[{"amount":"£1.57","description":"VAT"}]}}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RoundDown

func RoundDown(f float64) int64

RoundDown is a standard rounding function that always rounds down.

func RoundHalfDown

func RoundHalfDown(f float64) int64

RoundHalfDown is a standard rounding function that rounds 0.5 and below down.

func RoundHalfToEven

func RoundHalfToEven(f float64) int64

RoundHalfToEven is a standard rounding function that rounds 0.5 to the nearest even number. This is sometimes called bankers rounding.

func RoundHalfUp

func RoundHalfUp(f float64) int64

RoundHalfUp is a standard rounding function that rounds 0.5 and above up.

func RoundUp

func RoundUp(f float64) int64

RoundUp is a standard rounding function that always rounds up.

Types

type Money

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

Money is the main structure that holds a monetary value and how to format it as a string.

func MoneyEUR

func MoneyEUR[T constraints.Integer](value T) (Money, error)

MoneyEUR is a helper function.

func MoneyFromFloat added in v1.2.2

func MoneyFromFloat[T constraints.Float](currIsoCode string, value T, f roundFunc) (Money, error)

MoneyFromFloat constructs a new money object from a floating point number. currIsoCode is an ISO 4217 currency code. value is monetary value expressed as a float. roundFunc is a function to be used for division operations.

func MoneyFromString

func MoneyFromString(currIsoCode string, str string, f roundFunc) (Money, error)

MoneyFromString constructs a new money object from a string. Everything not contained within a number is stripped out before parsing. currIsoCode is an ISO 4217 currency code. str is monetary value expressed as a string. roundFunc is a function to be used for division operations.

func MoneyFromSubunits

func MoneyFromSubunits[T constraints.Integer](currIsoCode string, value T, f roundFunc) (Money, error)

MoneyFromSubunits constructs a new money object from an integer. The integer used should represent the subunits of the currency. currIsoCode is an ISO 4217 currency code. value is monetary value in subunits. roundFunc is a function to be used for division operations.

func MoneyGBP

func MoneyGBP[T constraints.Integer](value T) (Money, error)

MoneyGBP is a helper function.

func MoneyUSD added in v1.1.0

func MoneyUSD[T constraints.Integer](value T) (Money, error)

MoneyUSD is a helper function.

func (Money) Abs

func (m Money) Abs() Money

Abs returns a money object with an absolute value.

func (Money) Add

func (m Money) Add(v Money) Money

Add is an arithmetic operator.

func (Money) Allocate

func (m Money) Allocate(ratios ...int64) []Money

Allocate returns a slice containing money objects split according to the passed ratios. The ratios are completely arbitrary and are calculated as percentages of the overall sum. This operation is lossless and will account for all remainders.

func (Money) Clone added in v1.1.0

func (m Money) Clone(value int64) Money

Clone returns a copy of money with a different value.

func (Money) Div

func (m Money) Div(f float64) Money

Div is an arithmetic operator. This operation will perform rounding of the resulting value using the assigned rounding function. If you need to accurately divide a money object with lossless precision, use the Split or Allocate functions instead.

func (Money) Eq

func (m Money) Eq(v Money) bool

Eq is a logical operator.

func (Money) FlipSign

func (m Money) FlipSign() Money

FlipSign flips the sign of the money object's value. Switching positive to negative and vice versa.

func (Money) Gt

func (m Money) Gt(v Money) bool

Gt is a logical operator.

func (Money) Gte

func (m Money) Gte(v Money) bool

Gte is a logical operator.

func (Money) IsNeg

func (m Money) IsNeg() bool

IsNeg returns a boolean value if the value is negative.

func (Money) IsPos

func (m Money) IsPos() bool

IsPos returns a boolean value if the value is positive.

func (Money) IsZero

func (m Money) IsZero() bool

IsZero returns a boolean value if the value is zero.

func (Money) IsoCode

func (m Money) IsoCode() string

IsoCode returns the ISO 4217 currency code.

func (Money) Lt

func (m Money) Lt(v Money) bool

Lt is a logical operator.

func (Money) Lte

func (m Money) Lte(v Money) bool

Lte is a logical operator.

func (Money) MarshalJSON

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

MarshalJSON is an implementation of json.Marshaller.

func (Money) Mul

func (m Money) Mul(n int64) Money

Mul is an arithmetic operator.

func (Money) Neq

func (m Money) Neq(v Money) bool

Neq is a logical operator.

func (Money) Split

func (m Money) Split(n int64) []Money

Split returns a slice containing money objects split as evenly as possible by 'n' times. This operation is lossless and will account for all remainders.

func (Money) String

func (m Money) String() string

String is an implementation of fmt.Stringer and returns the string formatted representation of the monetary value.

func (Money) StringNoSymbol

func (m Money) StringNoSymbol() string

StringNoSymbol returns the string formatted representation of the monetary value without a currency symbol.

func (Money) Sub

func (m Money) Sub(v Money) Money

Sub is an arithmetic operator.

func (Money) Subunits

func (m Money) Subunits() int64

Subunits returns only the monetary subunits.

func (Money) Units

func (m Money) Units() int64

Units returns only the monetary units.

func (Money) Value

func (m Money) Value() int64

Value returns the entire monetary value expressed in subunits. For example, using GBP this would be pence, using EUR would be cents.

type Price

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

Price is a structure that holds a price and gives information about the amount of tax applied to that price.

func PriceFromFloat added in v1.2.2

func PriceFromFloat[T constraints.Float](currIsoCode string, gross T, f roundFunc) (Price, error)

PriceFromFloat constructs a new price object from a floating point number. currIsoCode is an ISO 4217 currency code. gross is monetary value expressed as a float. roundFunc is a function to be used for division operations.

func PriceFromString

func PriceFromString(currIsoCode string, gross string, f roundFunc) (Price, error)

PriceFromString constructs a new price object from a string. Everything not contained within a number is stripped out before parsing. currIsoCode is an ISO 4217 currency code. gross is monetary value expressed as a string. roundFunc is a function to be used for division operations.

func PriceFromSubunits

func PriceFromSubunits[T constraints.Integer](currIsoCode string, gross T, f roundFunc) (Price, error)

PriceFromSubunits constructs a new price object from an integer. currIsoCode is an ISO 4217 currency code. gross is monetary value in subunits. roundFunc is a function to be used for division operations.

func PriceGBP

func PriceGBP[T constraints.Integer](gross T, vat float64) (Price, error)

PriceGBP is a helper function. gross is the gross monetary value in subunits. vat is a tax percentage that's included in the gross value.

func (Price) Add added in v1.1.4

func (p Price) Add(v Price) Price

Add is an arithmetic operator.

func (*Price) AddTax added in v1.1.4

func (p *Price) AddTax(m Money, desc string)

AddTax adds a tax to the price using a money value. This will literally add the money amount to the gross price.

func (*Price) AddTaxPercent added in v1.1.0

func (p *Price) AddTaxPercent(percent float64, desc string)

AddTaxPercentage adds a tax to the price using a percentage. This will literally add a percentage to the gross price.

func (Price) Gross

func (p Price) Gross() Money

Gross returns the gross monetary value of the price.

func (*Price) IncludeTax added in v1.1.4

func (p *Price) IncludeTax(m Money, desc string)

IncludeTax adds a tax to the price using a money value. This implies this tax is already included in the gross price.

func (*Price) IncludeTaxPercent added in v1.1.0

func (p *Price) IncludeTaxPercent(percent float64, desc string)

IncludeTaxPercent adds a tax to the price using a percentage. This implies this tax is already included in the gross price.

func (Price) IsoCode

func (p Price) IsoCode() string

IsoCode returns the ISO 4217 currency code.

func (Price) MarshalJSON

func (p Price) MarshalJSON() ([]byte, error)

MarshalJSON is an implementation of json.Marshaller.

func (Price) Mul added in v1.1.4

func (p Price) Mul(n int64) Price

Mul is an arithmetic operator.

func (Price) Net

func (p Price) Net() Money

Net returns the net monetary value of the price which is equal to the gross minus tax.

func (Price) String

func (p Price) String() string

String is an implementation of fmt.Stringer and returns the string formatted representation of the price value.

func (Price) StringNoSymbol

func (p Price) StringNoSymbol() string

StringNoSymbol returns the string formatted representation of the price value without a currency symbol.

func (Price) Tax

func (p Price) Tax() Money

Tax returns the total amount of tax subtracted from the gross to produce the net.

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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