gocurrency

package module
v0.0.0-...-e22f1db Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2017 License: BSD-3-Clause Imports: 6 Imported by: 0

README

gocurrency

gocurrency is a minimalistic currency library for go.

Exchange rate sources

Current exchange rates are fetched from the european central bank. The base currency is euro. The exchange list will be updated every day by the ecb. It will be locally cached with a cache expire of 4 hours. Fetches are made automatically when you use the library.

Example

The library supports both, raw calculations and type-based usage.

//Money value to start with
money := 13.37

//Convert from euros to us dollars (USD)
money = ConvertEURTo("USD", money)

//Convert from us dollars (USD) to singapore dollars (SGD)
money = ConvertFromTo("USD", "SGD", money)

//Convert from singapore dollars (SGD) to euros (EUR)
money = ConvertToEUR("SGD", money)

//Check if the money is valid (valid means not 0 and not infinite)
println(IsNumberValid(money))

//Print the number formatted with 2 floating points
println(FormatNumber(money, 2))

Type based usage

//Money object to start with, requires the money value and the currency of the money
tmon := Money{Value: 13.37, Currency: "EUR"}

//Convert from euros to us dollars (USD)
tmon.To("USD")

//Convert from us dollars (USD) to singapore dollars (SGD)
tmon.To("SGD")

//Convert from singapore dollars (SGD) to euros (EUR)
tmon.ToEUR()

//Check if the money is valid (valid means not 0 and not infinite)
println(tmon.IsValid())

//Print the number formatted with 2 floating points
println(tmon.Format(2))

Info

This project is licensed under BSD 3-clause "New" or "Revised" license. Feel free to contribute.

Special thanks to @beevik for etree (xml library)

Documentation

Index

Constants

View Source
const (
	RatesSource = "http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"
	CacheExpire = time.Hour * 4
)

Variables

View Source
var (
	ExchangeRates = make(map[string]float64)
	CacheTime     time.Time
)

Functions

func ConvertEURTo

func ConvertEURTo(targetCurrency string, value float64) float64

Convert EUR to a specific currency; Returns 0 if currency doesn't exist

func ConvertFromTo

func ConvertFromTo(fromCurrency, toCurrency string, value float64) float64

Convert from a specific currency to a specific currency; Returns (-)infinite or 0 if currency doesn't exist.

func ConvertToEUR

func ConvertToEUR(currency string, value float64) float64

Convert the spcified currency to EUR; Returns (-)infinite float64 if currency doesn't exist,

func FormatNumber

func FormatNumber(value float64, precision int) string

Format a float64 number

func GetRate

func GetRate(currency string) float64

Return the rate of a currency; Returns default float64 if it can't find the currency

func IsNumberValid

func IsNumberValid(value float64) bool

Check if value is infinite or 0 (float default)

func RefrashIfRequired

func RefrashIfRequired()

func RefrashRates

func RefrashRates()

Types

type Money

type Money struct {
	Value    float64
	Currency string
}

func (*Money) Format

func (m *Money) Format(precision int) string

func (*Money) IsValid

func (m *Money) IsValid() bool

func (*Money) To

func (m *Money) To(currency string)

func (*Money) ToEUR

func (m *Money) ToEUR()

Jump to

Keyboard shortcuts

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