dinero

package module
Version: v0.0.0-...-1492fc4 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2017 License: MIT Imports: 6 Imported by: 0

README

dinero

dinero is a Go package for fetching exchange rates and converting between currencies. It aims to provide a simple interface and uses hippasus' ExchangeRates and decimal, which allows for higher precision when working with the values representing currency.

Build Status Go Report Card GoDoc

Installation

go get github.com/calmandniceperson/dinero

Examples

Converting a value in one currency to another
a := Amount{Value: decimal.NewFromFloat(5.245), Currency: USD}
// or
a := NewAmount(decimal.NewFromFloat(5.245), USD)
res, _ := a.ConvertTo(EUR)
// res contains the converted value as a decimal
Creating an amount from a float
a := NewAmountFromFloat(5.232, EUR)
Creating an amount from a string
a := NewAmountFromString("5423.65", JPY)
Creating a certain amount of a currency
u := USD
a1 := u.Amount(decimal.NewFromFloat(25000))
a2 := u.AmountFromFloat(7300.32)
a3 := u.AmountFromString("200.09")
Printing an amount of a certain currency
a := Amount{decimal.NewFromFloat(450), JPY}
println(a.String())

Dependencies

For testing

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func URL

func URL(from, to string) string

URL generates a URL for querying the ExchangeRates API.

Types

type Amount

type Amount struct {
	Value    decimal.Decimal
	Currency Currency
}

Amount represents a money value in a given currency.

func NewAmount

func NewAmount(value decimal.Decimal, curr Currency) Amount

NewAmount returns an Amount with the given value and currency. It's sort of a helper function, making the creation of an Amount simpler.

func NewAmountFromFloat

func NewAmountFromFloat(value float64, curr Currency) Amount

NewAmountFromFloat returns an Amount with the given value (provided as float) and currency. It's sort of a helper function, making the creation of an Amount from a float simpler.

func NewAmountFromString

func NewAmountFromString(value string, curr Currency) (Amount, error)

NewAmountFromString tries creating a new amount from the provided string. Should the conversion fail, an error is returned. If the conversion was successful, the function returns the new Amount.

func (Amount) ConvertTo

func (a Amount) ConvertTo(target Currency) (decimal.Decimal, error)

ConvertTo converts Amount a to another target currency. It returns the converted value as a float64 or an error.

func (Amount) ConvertToMv

func (a Amount) ConvertToMv(target Currency) (*Amount, error)

ConvertToMv converts Amount a to another target currency. It returns the converted Amount or an error.

func (Amount) String

func (a Amount) String() string

String returns the amount with its currency symbol in the predefined format "<value> <symbol>".

type Currency

type Currency uint8

Currency reprents a currency as an integer value. This is used as sort of an enum in which the constant values defined as currencies may be used by users of the library and at the same time are used as indeces for the currency code array.

const (

	// USD -> USDollar ($ / USD)
	USD Currency = iota

	// EUR -> Euro (€ / EUR)
	EUR

	// JPY -> JapaneseYen (¥ / JPY)
	JPY

	// BGN -> BulgarianLev (лв. / BGN)
	BGN

	// CZK -> CzechKoruna (Kč / CZK)
	CZK

	// DKK -> DanishKrone (kr. / DKK)
	DKK

	// GBP -> GreatBritainPound (£ / GBP)
	GBP

	// HUF -> HungarianForint (Ft / HUF)
	HUF

	// LTL -> LithuanianLitas (Lt / LTL)
	LTL

	// LVL -> LatvianLats (Ls / LVL)
	LVL

	// PLN -> PolishZloty (zł / PLN)
	PLN

	// RON -> RomanianLeu (? / RON)
	RON

	// SEK -> SwedishKrona (kr / SEK)
	SEK

	// CHF -> SwissFranc (CHF / CHF)
	CHF

	// NOK -> NorwegianKrone (kr / NOK)
	NOK

	// HRK -> CroatianKuna (kn / HRK)
	HRK

	// RUB -> RussianRubel (₽ / RUB)
	RUB

	// TRY -> TurkishLira (₺ / TRY)
	TRY

	// AUD -> AustralianDollar ($, A$ or AUD / AUD)
	AUD

	// BRL -> BrazilianReal (R$ / BRL)
	BRL

	// CAD -> CanadianDollar ($, Can$, C$ or CAD / CAD)
	CAD

	// CNY -> ChineseYuan (¥ / CNY)
	CNY

	// HKD -> HongKongDollar ($ or HK$ / HKD)
	HKD

	// IDR -> IndonesianRupiah (Rp / IDR)
	IDR

	// ILS -> IsraeliNewShekel (₪  / ILS)
	ILS

	// INR -> IndianRupee (₹ / INR)
	INR

	// KRW -> SouthKoreanWon (₩ / KRW)
	KRW

	// MXN -> MexicanPeso ($ or Mex$ / MXN)
	MXN

	// MYR -> MalaysianRinggit (RM / MYR)
	MYR

	// NZD -> NewZealandDollar ($ / NZD)
	NZD

	// PHP -> PhilippinePeso (₱ / PHP)
	PHP

	// SGD -> SingaporeDollar (S$ or $ / SGD)
	SGD

	// THB -> ThaiBaht (฿ / THB)
	THB

	// ZAR -> SouthAfricanRand (R / ZAR)
	ZAR

	// ISK -> IcelandicKrona (kr or Íkr / ISK)
	ISK
)

func (Currency) Amount

func (c Currency) Amount(value decimal.Decimal) Amount

Amount creates an Amount for the given currency unit and amount.

func (Currency) AmountFromFloat

func (c Currency) AmountFromFloat(value float64) Amount

AmountFromFloat creates an amount for the given currency unit and a value, which is provided as a float64.

func (Currency) AmountFromString

func (c Currency) AmountFromString(value string) (Amount, error)

AmountFromString creates an amount for the given currency unit and a value, which is provided as a string.

func (Currency) ExchangeRate

func (c Currency) ExchangeRate(target Currency) (decimal.Decimal, error)

ExchangeRate returns the exchance rate when exchanging money from the currency c to the target currency or an error.

func (Currency) ExchangeRateFloat

func (c Currency) ExchangeRateFloat(target Currency) (rate float64, exact bool, err error)

ExchangeRateFloat returns the exchange rate when exchanging money from the currency c to the target currency as a float64 or an error.

Jump to

Keyboard shortcuts

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