money

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2017 License: MIT Imports: 5 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Money

alt text

GoMoney provides ability to work with monetary value using a currency's smallest unit. Package allows you to use basic Money operations like rounding, splitting or allocating without losing a penny. You shouldn't use float for monetary values, since they always carry small rounding differences.

package main

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

func main() {
	pound := money.New(100, "GBP")
	twoPounds := pound.Add(pound)

	parties := twoPounds.Split(3)
	parties[0].format() // £0.67
	parties[1].format() // £0.67
	parties[2].format() // £0.66
}

Quick start

Get the package via

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

Features

  • Provides a Money struct which stores information about an Money amount value and it's 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

Init
// Initialise Money by using smallest unit value (e.g 100 represents 1 pound)
// and use ISO 4217 Currency Code to set money Currency
pound := money.New(100, "GBP")
Comparison

Go-money lets you to use base compare operations like:

  • Equals
  • GreaterThan
  • GreaterThanOrEqual
  • LessThan
  • LessThanOrEqual In order to use them currencies must be equal
// Initialise Money by using smallest unit value (e.g 100 represents 1 pound)
// and use ISO 4217 Currency Code to set money Currency
pound := money.New(100, "GBP")
twoPounds := money.New(200, "GBP")
twoEuros := money.New(200, "EUR")

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

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
}

type Calculator

type Calculator struct{}

type Currencies

type Currencies map[string]*Currency

type Currency

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

func (*Currency) Equals

func (c *Currency) Equals(oc *Currency) bool

func (*Currency) Get

func (c *Currency) Get(code string) *Currency

type Formatter

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

func NewFormatter

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

func (*Formatter) Format

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

type Money

type Money struct {
	Amount   *Amount
	Currency *Currency
}

func New

func New(amount int, currency string) *Money

New creates and returns new instance of Money

func (*Money) Absolute

func (m *Money) Absolute() *Money

func (*Money) Add

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

func (*Money) Allocate

func (m *Money) Allocate(rs []int) []*Money

func (*Money) Divide

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

func (*Money) Equals

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

func (*Money) Format

func (m *Money) Format() string

func (*Money) GreaterThan

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

func (*Money) GreaterThanOrEqual

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

func (*Money) IsNegative

func (m *Money) IsNegative() bool

func (*Money) IsPositive

func (m *Money) IsPositive() bool

func (*Money) IsZero

func (m *Money) IsZero() bool

func (*Money) LessThan

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

func (*Money) LessThanOrEqual

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

func (*Money) Multiply

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

func (*Money) Negative

func (m *Money) Negative() *Money

func (*Money) Round

func (m *Money) Round() *Money

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

func (*Money) Subtract

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

Jump to

Keyboard shortcuts

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