money

package module
v0.3.7 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2018 License: MIT Imports: 3 Imported by: 0

README

Money

alt text

Go Report Card Coverage Status Build Status GoDoc

GoMoney provides ability to work with monetary value using a currency's smallest unit. This package provides basic and precise Money operations such as rounding, splitting and allocating. Monetary values should not be stored as floats due to small rounding differences.

package main

import "github.com/rhymond/go-money"

func main() {
    pound := money.New(100, "GBP")
    twoPounds, err := pound.Add(pound)
    
    if err != nil {
        log.Fatal(err)
    }
    
    parties, err := twoPounds.Split(3)
        
    if err != nil {
        log.Fatal(err)
    }
    
    parties[0].Display() // £0.67
    parties[1].Display() // £0.67
    parties[2].Display() // £0.66
}

Quick start

Get the package:

$ go get github.com/rhymond/go-money

Features

  • Provides a Money struct which stores information about an Money amount value and its currency.
  • Provides a Money.Amount struct which encapsulates all information about a monetary unit.
  • Represents monetary values as integers, in cents. This avoids floating point rounding errors.
  • Represents currency as Money.Currency instances providing a high level of flexibility.

Usage

Initialization

Initialize Money by using smallest unit value (e.g 100 represents 1 pound). Use ISO 4217 Currency Code to set money Currency

pound := money.New(100, "GBP")

Comparison

Go-money provides base compare operations like:

  • Equals
  • GreaterThan
  • GreaterThanOrEqual
  • LessThan
  • LessThanOrEqual

Comparisons must be made between the same currency units.

pound := money.New(100, "GBP")
twoPounds := money.New(200, "GBP")
twoEuros := money.New(200, "EUR")

pound.GreaterThan(twoPounds) // false, nil
pound.LessThan(twoPounds) // true, nil
twoPounds.Equals(twoEuros) // false, error: Currencies don't match

Asserts

  • IsZero
  • IsNegative
  • IsPositive
Zero value

To assert if Money value is equal to zero use IsZero()

pound := money.New(100, "GBP")
result := pound.IsZero(pound) // false
Positive value

To assert if Money value is more than zero use IsPositive()

pound := money.New(100, "GBP")
pound.IsPositive(pound) // true
Negative value

To assert if Money value is less than zero use IsNegative()

pound := money.New(100, "GBP")
pound.IsNegative(pound) // false

Operations

  • Add
  • Subtract
  • Divide
  • Multiply
  • Absolute
  • Negative

Comparisons must be made between the same currency units.

Addition

Additions can be performed using Add().

pound := money.New(100, "GBP")
twoPounds := money.New(200, "GBP")
 
result, err := pound.Add(twoPounds) // £3.00, nil
Subtraction

Subtraction can be performed using Subtract().

pound := money.New(100, "GBP")
twoPounds := money.New(200, "GBP")
 
result, err := pound.Subtract(twoPounds) // -£1.00, nil
Multiplication

Multiplication can be performed using Multiply().

pound := money.New(100, "GBP")
 
result := pound.Multiply(2) // £2.00
Division

Division can be performed using Divide().

pound := money.New(100, "GBP")
 
result := pound.Divide(2) // £0.50

There is possibilities to lose pennies by using division operation e.g:

money.New(100, "GBP").Divide(3) // £0.33

In order to split amount without losing use Split() operation.

Absolute

Return absolute value of Money structure

pound := money.New(-100, "GBP")
 
result := pound.Absolute() // £1.00
Negative

Return negative value of Money structure

pound := money.New(100, "GBP")
 
result := pound.Negative() // -£1.00

Allocation

  • Split
  • Allocate
Splitting

In order to split Money for parties without losing any pennies due to rounding differences, use Split().

After division leftover pennies will be distributed round-robin amongst the parties. This means that parties listed first will likely receive more pennies than ones that are listed later.

pound := money.New(100, "GBP")
parties, err := pound.Split(3)

if err != nil {
    log.Fatal(err)
}

parties[0].Display() // £0.34
parties[1].Display() // £0.33
parties[2].Display() // £0.33
Allocation

To perform allocation operation use Allocate().

It splits money using the given ratios without losing pennies and as Split operations distributes leftover pennies amongst the parties with round-robin principle.

pound := money.New(100, "GBP")
// Allocate is variadic function which can receive ratios as
// slice (int[]{33, 33, 33}...) or separated by a comma integers
parties, err := pound.Allocate(33, 33, 33)

if err != nil {
    log.Fatal(err)
}

parties[0].Display() // £0.34
parties[1].Display() // £0.33
parties[2].Display() // £0.33

Format

To format and return Money as a string use Display().

money.New(123456789, "EUR").Display() // €1,234,567.89

Contributing

Thank you for considering contributing! Please use GitHub issues and Pull Requests for contributing.

License

The MIT License (MIT). Please see License File for more information.

forthebadge

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Amount

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

Amount is a datastructure that stores the amount being used for calculations

type Currency

type Currency struct {
	Code     string
	Fraction int
	Grapheme string
	Template string
	Decimal  string
	Thousand string
}

Currency represents money currency information required for formatting

func AddCurrency added in v0.2.0

func AddCurrency(Code, Grapheme, Template, Decimal, Thousand string, Fraction int) *Currency

AddCurrency lets you insert or update currency in currencies list

func GetCurrency added in v0.3.5

func GetCurrency(code string) *Currency

GetCurrency returns the currency given the code.

func (*Currency) Formatter added in v0.2.0

func (c *Currency) Formatter() *Formatter

Formatter returns currency formatter representing used currency structure

type Formatter

type Formatter struct {
	Fraction int
	Decimal  string
	Thousand string
	Grapheme string
	Template string
}

Formatter stores Money formatting information

func NewFormatter

func NewFormatter(fraction int, decimal, thousand, grapheme, template string) *Formatter

NewFormatter creates new Formatter instance

func (*Formatter) Format

func (f *Formatter) Format(amount int64) string

Format returns string of formatted integer using given currency template

type Money

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

Money represents monetary value information, stores currency and amount value

func New

func New(amount int64, code string) *Money

New creates and returns new instance of Money

func (*Money) Absolute

func (m *Money) Absolute() *Money

Absolute returns new Money struct from given Money using absolute monetary value

func (*Money) Add

func (m *Money) Add(om *Money) (*Money, error)

Add returns new Money struct with value representing sum of Self and Other Money

func (*Money) Allocate

func (m *Money) Allocate(rs ...int) ([]*Money, error)

Allocate returns slice of Money structs with split Self value in given ratios. It lets split money by given ratios without losing pennies and as Split operations distributes leftover pennies amongst the parties with round-robin principle.

func (*Money) Amount

func (m *Money) Amount() int64

Amount returns a copy of the internal monetary value as an int64

func (*Money) Currency

func (m *Money) Currency() *Currency

Currency returns the currency used by Money

func (*Money) Display added in v0.1.0

func (m *Money) Display() string

Display lets represent Money struct as string in given Currency value

func (*Money) Divide

func (m *Money) Divide(div int64) *Money

Divide returns new Money struct with value representing Self division value by given divider

func (*Money) Equals

func (m *Money) Equals(om *Money) (bool, error)

Equals checks equality between two Money types

func (*Money) GreaterThan

func (m *Money) GreaterThan(om *Money) (bool, error)

GreaterThan checks whether the value of Money is greater than the other

func (*Money) GreaterThanOrEqual

func (m *Money) GreaterThanOrEqual(om *Money) (bool, error)

GreaterThanOrEqual checks whether the value of Money is greater or equal than the other

func (*Money) IsNegative

func (m *Money) IsNegative() bool

IsNegative returns boolean of whether the value of Money is negative

func (*Money) IsPositive

func (m *Money) IsPositive() bool

IsPositive returns boolean of whether the value of Money is positive

func (*Money) IsZero

func (m *Money) IsZero() bool

IsZero returns boolean of whether the value of Money is equals to zero

func (*Money) LessThan

func (m *Money) LessThan(om *Money) (bool, error)

LessThan checks whether the value of Money is less than the other

func (*Money) LessThanOrEqual

func (m *Money) LessThanOrEqual(om *Money) (bool, error)

LessThanOrEqual checks whether the value of Money is less or equal than the other

func (*Money) Multiply

func (m *Money) Multiply(mul int64) *Money

Multiply returns new Money struct with value representing Self multiplied value by multiplier

func (*Money) Negative

func (m *Money) Negative() *Money

Negative returns new Money struct from given Money using negative monetary value

func (*Money) Round

func (m *Money) Round() *Money

Round returns new Money struct with value rounded to nearest zero

func (*Money) SameCurrency

func (m *Money) SameCurrency(om *Money) bool

SameCurrency check if given Money is equals by currency

func (*Money) Split

func (m *Money) Split(n int) ([]*Money, error)

Split returns slice of Money structs with split Self value in given number. After division leftover pennies will be distributed round-robin amongst the parties. This means that parties listed first will likely receive more pennies than ones that are listed later

func (*Money) Subtract

func (m *Money) Subtract(om *Money) (*Money, error)

Subtract returns new Money struct with value representing difference of Self and Other Money

Jump to

Keyboard shortcuts

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