mongo

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2022 License: MIT Imports: 6 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.go.dev/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","formatted":"£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":"£1.57","taxPercent":17.500000}

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 round 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 round 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(value int64) (Money, error)

MoneyEUR is a helper function.

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. value is monetary value in subunits. roundFunc is a function to be used for division operations.

func MoneyFromSubunits

func MoneyFromSubunits(currIsoCode string, value int64, 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(value int64) (Money, error)

MoneyGBP is a helper function.

func MoneyUSD added in v1.1.0

func MoneyUSD(value int64) (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 PriceFromString

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

MoneyFromString constructs a new price object from a string and tax percentage. Everything not contained within a number is stripped out before parsing. currIsoCode is an ISO 4217 currency code. value is monetary value in subunits. roundFunc is a function to be used for division operations.

func PriceFromSubunits

func PriceFromSubunits(currIsoCode string, grossValue int64, f roundFunc) (Price, error)

PriceFromSubunits constructs a new price object from an integer and tax percentage. The value 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 PriceGBP

func PriceGBP(grossValue int64, vat float64) (Price, error)

PriceGBP is a helper function.

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) AddTaxSubunits added in v1.1.0

func (p *Price) AddTaxSubunits(value int64, desc string)

AddTaxSubunits adds a tax to the price using subunits. This will literally add a subunit amount to the gross price.

func (Price) Gross

func (p Price) Gross() Money

Gross returns the gross monetary value of the price.

func (*Price) IncludeTaxPercent added in v1.1.0

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

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

func (*Price) IncludeTaxSubunits added in v1.1.0

func (p *Price) IncludeTaxSubunits(value int64, desc string)

IncludeTaxSubunits adds a tax to the price using subunits. 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) 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.

Jump to

Keyboard shortcuts

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