money

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2020 License: MIT Imports: 7 Imported by: 0

README

Golang Money

Documentation Go Report Card

Money is value type built from currency code and money value in minor units:

type Money struct {
	Amount   int64
	Currency string
}

Currencies scales (or "exponents") are package-level constants from ISO4217 data file. For unknown currencies scale '2' is used.

Usage examples

Money creation & representation
_1usd := money.Make(1, 0, "USD")
fmt.Println(_1usd)

_1usd = money.Make(100, 2, "USD")
fmt.Println(_1usd)

_10usd := money.FromFloat(10.001, "USD")
fmt.Println(_10usd)

_100usd, _ := money.FromString("100.00 USD")
fmt.Println(_100usd)

//Output:
// 1.00 USD
// 1.00 USD
// 10.00 USD
// 100.00 USD
Money Sum
converter := money.RatesTableConverter{
    "USD": 1,
    "RUB": 60,
}
var sum money.Sum
sum.Add(money.FromFloat(100, "USD"))
sum.Add(money.FromFloat(600, "RUB"))
sumValue, _ := sum.Calculate("USD", converter)
fmt.Println(sumValue)

// Output: 110.00 USD

TODO

  • Distribute

Documentation

Overview

Example
package main

import (
	"fmt"

	"github.com/gruzovator/money"
)

func main() {
	_1usd := money.Make(1, 0, "USD")
	fmt.Println(_1usd)

	_1usd = money.Make(100, 2, "USD")
	fmt.Println(_1usd)

	_10usd := money.FromFloat(10.001, "USD")
	fmt.Println(_10usd)

	_100usd, _ := money.FromString("100.00 USD")
	fmt.Println(_100usd)

}
Output:

1.00 USD
1.00 USD
10.00 USD
100.00 USD

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CurrencyScalesFromISO4217

func CurrencyScalesFromISO4217(xmlContentReader io.Reader) (map[string]int, error)

CurrencyScalesFromISO4217 parses currencies codes and scales from ISO417 XML data.

See:

func DefaultScale

func DefaultScale() int

DefaultScale is scale value used for unknown currencies.

func ForEachCurrency

func ForEachCurrency(cb func(code string, scale int))

ForEachCurrency is for currencies data iteration.

func IsKnownCurrency

func IsKnownCurrency(curecnyCode string) bool

IsKnownCurrency checks if currency presents in ISO4217 data.

func ReplaceCurrenciesSystem

func ReplaceCurrenciesSystem(scaleByCode map[string]int, defaultScale int)

ReplaceCurrenciesSystem should be used to replace package currencies data.

func ScaleForCurrency

func ScaleForCurrency(currencyCode string) int

ScaleForCurrency gives scale value for currency code. Default scale is used for unknown currency.

Types

type Converter

type Converter interface {
	Convert(money Money, toCurrency string) (Money, error)
}

Converter is a currency converter

type Money

type Money struct {
	Amount   int64
	Currency string
}

Money is amount of money as integer number of minor units and currency code.

func FromFloat

func FromFloat(val float64, currencyCode string) Money

FromFloat create Money instance rounding float value according to currency scale.

func FromString

func FromString(s string) (Money, error)

FromString parses Money from string that has two parts: float value and currency code.

func Make

func Make(amount int64, scale int, currencyCode string) Money

Make creates Money by normalizing amount/scale according to currencies system. E.g. Money(100, 0, "USD") gives Money{100_00, "USD"}. Rounding is done via math.Round.

func (Money) String

func (m Money) String() string

String creates string representation, e.g. "100.00 USD" or "100 KRW" (currency with scale 0).

type RatesTableConverter

type RatesTableConverter map[string]float64

func (RatesTableConverter) Convert

func (c RatesTableConverter) Convert(m Money, toCurrency string) (Money, error)

type Sum

type Sum struct {
	AmountsByCurrency map[string]int64
}

Sum aggregates money values by currency.

func (*Sum) Add

func (s *Sum) Add(m Money)

func (*Sum) Calculate

func (s *Sum) Calculate(targetCurrency string, converter Converter) (Money, error)

Calcuelate calculates sum value in target currency.

Example
package main

import (
	"fmt"

	"github.com/gruzovator/money"
)

func main() {
	converter := money.RatesTableConverter{
		"USD": 1,
		"RUB": 60,
	}
	var sum money.Sum
	sum.Add(money.FromFloat(100, "USD"))
	sum.Add(money.FromFloat(600, "RUB"))
	sumValue, _ := sum.Calculate("USD", converter)
	fmt.Println(sumValue)

}
Output:

110.00 USD

Jump to

Keyboard shortcuts

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