money

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

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

Go to latest
Published: Sep 14, 2022 License: MIT Imports: 12 Imported by: 0

README

Money

Money is a Go package that helps you to work with currencies. It is based on Fowler's Money pattern and provides the ability to work with monetary value using a currency's smallest unit. So with this package, you can perform precise operations without being afraid of losing a single cent.

Money supports currency exchange so you can convert easily, for example, 1 dollar in 1 euro.

Usage

package main

import (
  "fmt"
  "github.com/pioz/money"
)

func main() {
  m1, _ := money.NewMoney(100, "USD")
  m2, _ := money.NewMoneyFromAmount(4.00, "USD")
  m, _ := m1.Add(m2)
  fmt.Println(m.Format()) // Output: $5.00
  m = m.Multiply(2)
  fmt.Println(m.Format()) // Output: $10.00

  parts, _ := m.Split(3)
  fmt.Println(parts[0].Format()) // Output: $3.34
  fmt.Println(parts[1].Format()) // Output: $3.33
  fmt.Println(parts[2].Format()) // Output: $3.33
}

Bank

A bank is a type in the money package that allows you to create money. A bank define in which currencies you can create money and the exchange rates table to exchange monetary value in a currency to another. A sample exchange rates table is this:

exchangeRatesTable := map[string]map[string]float64{
  "USD": { "EUR": 0.87, "GBP": 0.74 },
  "EUR": { "USD": 1.16, "GBP": 0.86 },
  "GBP": { "USD": 1.35, "EUR": 1.17 },
}

And here is an example of how to use a bank:

package main

import (
  "fmt"
  "log"
  "github.com/pioz/money"
)

func main() {
  fetchExchangeRatesTable := func() (money.ExchangeRatesTable, error) {
    var table money.ExchangeRatesTable
    // fetch table from Internet
    return table, nil
  }
  bank, _ := money.NewBank([]money.Currency{money.EUR, money.USD}, fetchExchangeRatesTable, nil)
  usd, _ := bank.NewMoney(100, "USD")
  eur, _ := usd.ExchangeTo("EUR")
  fmt.Println(eur.Format())

  bank.UpdateExchangeRatesTable() // Update exchange rates table

  eur, _ = usd.ExchangeTo("EUR")
  fmt.Println(eur.Format())

  _, err := bank.NewMoney(1, "JPY")
  if err != nil {
    log.Println(err.Error()) // Output: Bank does not support JPY currency
  }
}

You can create a bank with an ExchangeRatesTableCache that load the exchange rates table from this cache, while a go routine in background updates the exchange rates table.

  fetchExchangeRatesTable := func() (money.Rates, error) {
    var table money.ExchangeRatesTable
    // fetch table from Internet
    return table, nil
  }
  fileCache := money.ExchangeRatesTableFileCache{FilePath: "/tmp/go-money-exchange-rates-table-cache"}
  bank, _ := money.NewBank(money.AllCurrencies, fetchExchangeRatesTable, fileCache)
  usd, _ := bank.NewMoney(100, "USD")
  eur, _ := usd.ExchangeTo("EUR")
  fmt.Println(eur.Format())

Money package provides a simple file cache type that read and write the exchange rates table in a file, but you can create your custom type simply by implementing the ExchangeRatesTableCache interface, for example, to use Redis or something else.

Freecurrency bank

Money package allows you to create out of the box a bank that can fetch the exchange rates table from https://freecurrencyapi.net/

import "github.com/pioz/money/banks"

func main() {
  bank, err := banks.NewFreecurrencyBank([]money.Currency{money.EUR, money.USD}, "YOUR-API-KEY", nil)
  if err != nil {
    panic(err)
  }
  m, _ := bank.NewMoney(100, "EUR")
	ex, _ := m.ExchangeTo("USD")
}

Currencies

The money package has all real-life currencies pre-defined. But you can also define a new currency for your realm 😅. See the go doc to see how the currency type is like.

Contributing

HELP: all contributions to improve the go doc are very very welcome!

Bug reports and pull requests are welcome on GitHub at https://github.com/pioz/money/issues.

License

The package is available as open source under the terms of the MIT License.

Documentation

Overview

Money is a Go package that helps you to work with currencies. It is based on Fowler's Money pattern and provides the ability to work with monetary value using a currency's smallest unit. Money supports currency exchange so you can convert easily, for example, 1 dollar in 1 euro.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	AED = Currency{Name: "United Arab Emirates Dirham", IsoCode: "AED", Symbol: "د.إ", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	AFN = Currency{Name: "Afghan Afghani", IsoCode: "AFN", Symbol: "؋", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	ALL = Currency{Name: "Albanian Lek", IsoCode: "ALL", Symbol: "L", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	AMD = Currency{Name: "Armenian Dram", IsoCode: "AMD", Symbol: "դր.", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	ANG = Currency{Name: "Netherlands Antillean Gulden", IsoCode: "ANG", Symbol: "ƒ", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: '.', DecimalMark: ','}
	AOA = Currency{Name: "Angolan Kwanza", IsoCode: "AOA", Symbol: "Kz", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	ARS = Currency{Name: "Argentine Peso", IsoCode: "ARS", Symbol: "$", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: '.', DecimalMark: ','}
	AUD = Currency{Name: "Australian Dollar", IsoCode: "AUD", Symbol: "A$", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	AWG = Currency{Name: "Aruban Florin", IsoCode: "AWG", Symbol: "ƒ", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	AZN = Currency{Name: "Azerbaijani Manat", IsoCode: "AZN", Symbol: "₼", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	BAM = Currency{Name: "Bosnia and Herzegovina Convertible Mark", IsoCode: "BAM", Symbol: "КМ", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	BBD = Currency{Name: "Barbadian Dollar", IsoCode: "BBD", Symbol: "$", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	BCH = Currency{Name: "Bitcoin Cash", IsoCode: "BCH", Symbol: "₿", SymbolFirst: false, SubunitToUnit: 100000000, ThousandsSeparator: ',', DecimalMark: '.'}
	BDT = Currency{Name: "Bangladeshi Taka", IsoCode: "BDT", Symbol: "৳", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	BGN = Currency{Name: "Bulgarian Lev", IsoCode: "BGN", Symbol: "лв.", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	BHD = Currency{Name: "Bahraini Dinar", IsoCode: "BHD", Symbol: "ب.د", SymbolFirst: true, SubunitToUnit: 1000, ThousandsSeparator: ',', DecimalMark: '.'}
	BIF = Currency{Name: "Burundian Franc", IsoCode: "BIF", Symbol: "Fr", SymbolFirst: false, SubunitToUnit: 1, ThousandsSeparator: ',', DecimalMark: '.'}
	BMD = Currency{Name: "Bermudian Dollar", IsoCode: "BMD", Symbol: "$", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	BND = Currency{Name: "Brunei Dollar", IsoCode: "BND", Symbol: "$", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	BOB = Currency{Name: "Bolivian Boliviano", IsoCode: "BOB", Symbol: "Bs.", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	BRL = Currency{Name: "Brazilian Real", IsoCode: "BRL", Symbol: "R$", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: '.', DecimalMark: ','}
	BSD = Currency{Name: "Bahamian Dollar", IsoCode: "BSD", Symbol: "$", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	BTC = Currency{Name: "Bitcoin", IsoCode: "BTC", Symbol: "₿", SymbolFirst: true, SubunitToUnit: 100000000, ThousandsSeparator: ',', DecimalMark: '.'}
	BTN = Currency{Name: "Bhutanese Ngultrum", IsoCode: "BTN", Symbol: "Nu.", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	BWP = Currency{Name: "Botswana Pula", IsoCode: "BWP", Symbol: "P", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	BYN = Currency{Name: "Belarusian Ruble", IsoCode: "BYN", Symbol: "Br", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ' ', DecimalMark: ','}
	BZD = Currency{Name: "Belize Dollar", IsoCode: "BZD", Symbol: "$", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	CAD = Currency{Name: "Canadian Dollar", IsoCode: "CAD", Symbol: "C$", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	CDF = Currency{Name: "Congolese Franc", IsoCode: "CDF", Symbol: "Fr", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	CHF = Currency{Name: "Swiss Franc", IsoCode: "CHF", Symbol: "CHF", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	CLF = Currency{Name: "Unidad de Fomento", IsoCode: "CLF", Symbol: "UF", SymbolFirst: true, SubunitToUnit: 10000, ThousandsSeparator: '.', DecimalMark: ','}
	CLP = Currency{Name: "Chilean Peso", IsoCode: "CLP", Symbol: "$", SymbolFirst: true, SubunitToUnit: 1, ThousandsSeparator: '.', DecimalMark: ','}
	CNH = Currency{Name: "Chinese Renminbi Yuan Offshore", IsoCode: "CNH", Symbol: "¥", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	CNY = Currency{Name: "Chinese Renminbi Yuan", IsoCode: "CNY", Symbol: "¥", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	COP = Currency{Name: "Colombian Peso", IsoCode: "COP", Symbol: "$", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: '.', DecimalMark: ','}
	CRC = Currency{Name: "Costa Rican Colón", IsoCode: "CRC", Symbol: "₡", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: '.', DecimalMark: ','}
	CUC = Currency{Name: "Cuban Convertible Peso", IsoCode: "CUC", Symbol: "$", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	CUP = Currency{Name: "Cuban Peso", IsoCode: "CUP", Symbol: "$", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	CVE = Currency{Name: "Cape Verdean Escudo", IsoCode: "CVE", Symbol: "$", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	CZK = Currency{Name: "Czech Koruna", IsoCode: "CZK", Symbol: "Kč", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ' ', DecimalMark: ','}
	DJF = Currency{Name: "Djiboutian Franc", IsoCode: "DJF", Symbol: "Fdj", SymbolFirst: false, SubunitToUnit: 1, ThousandsSeparator: ',', DecimalMark: '.'}
	DKK = Currency{Name: "Danish Krone", IsoCode: "DKK", Symbol: "kr.", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: '.', DecimalMark: ','}
	DOP = Currency{Name: "Dominican Peso", IsoCode: "DOP", Symbol: "$", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	DZD = Currency{Name: "Algerian Dinar", IsoCode: "DZD", Symbol: "د.ج", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	EEK = Currency{Name: "Estonian Kroon", IsoCode: "EEK", Symbol: "KR", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	EGP = Currency{Name: "Egyptian Pound", IsoCode: "EGP", Symbol: "ج.م", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	ERN = Currency{Name: "Eritrean Nakfa", IsoCode: "ERN", Symbol: "Nfk", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	ETB = Currency{Name: "Ethiopian Birr", IsoCode: "ETB", Symbol: "Br", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	EUR = Currency{Name: "Euro", IsoCode: "EUR", Symbol: "€", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: '.', DecimalMark: ','}
	FJD = Currency{Name: "Fijian Dollar", IsoCode: "FJD", Symbol: "$", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	FKP = Currency{Name: "Falkland Pound", IsoCode: "FKP", Symbol: "£", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	GBP = Currency{Name: "British Pound", IsoCode: "GBP", Symbol: "£", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	GBX = Currency{Name: "British Penny", IsoCode: "GBX", Symbol: "", SymbolFirst: true, SubunitToUnit: 1, ThousandsSeparator: ',', DecimalMark: '.'}
	GEL = Currency{Name: "Georgian Lari", IsoCode: "GEL", Symbol: "ლ", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	GGP = Currency{Name: "Guernsey Pound", IsoCode: "GGP", Symbol: "£", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	GHS = Currency{Name: "Ghanaian Cedi", IsoCode: "GHS", Symbol: "₵", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	GIP = Currency{Name: "Gibraltar Pound", IsoCode: "GIP", Symbol: "£", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	GMD = Currency{Name: "Gambian Dalasi", IsoCode: "GMD", Symbol: "D", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	GNF = Currency{Name: "Guinean Franc", IsoCode: "GNF", Symbol: "Fr", SymbolFirst: false, SubunitToUnit: 1, ThousandsSeparator: ',', DecimalMark: '.'}
	GTQ = Currency{Name: "Guatemalan Quetzal", IsoCode: "GTQ", Symbol: "Q", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	GYD = Currency{Name: "Guyanese Dollar", IsoCode: "GYD", Symbol: "$", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	HKD = Currency{Name: "Hong Kong Dollar", IsoCode: "HKD", Symbol: "$", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	HNL = Currency{Name: "Honduran Lempira", IsoCode: "HNL", Symbol: "L", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	HRK = Currency{Name: "Croatian Kuna", IsoCode: "HRK", Symbol: "kn", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: '.', DecimalMark: ','}
	HTG = Currency{Name: "Haitian Gourde", IsoCode: "HTG", Symbol: "G", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	HUF = Currency{Name: "Hungarian Forint", IsoCode: "HUF", Symbol: "Ft", SymbolFirst: false, SubunitToUnit: 1, ThousandsSeparator: ' ', DecimalMark: ','}
	IDR = Currency{Name: "Indonesian Rupiah", IsoCode: "IDR", Symbol: "Rp", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: '.', DecimalMark: ','}
	ILS = Currency{Name: "Israeli New Sheqel", IsoCode: "ILS", Symbol: "₪", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	IMP = Currency{Name: "Isle of Man Pound", IsoCode: "IMP", Symbol: "£", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	INR = Currency{Name: "Indian Rupee", IsoCode: "INR", Symbol: "₹", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	IQD = Currency{Name: "Iraqi Dinar", IsoCode: "IQD", Symbol: "ع.د", SymbolFirst: false, SubunitToUnit: 1000, ThousandsSeparator: ',', DecimalMark: '.'}
	IRR = Currency{Name: "Iranian Rial", IsoCode: "IRR", Symbol: "﷼", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	ISK = Currency{Name: "Icelandic Króna", IsoCode: "ISK", Symbol: "kr", SymbolFirst: true, SubunitToUnit: 1, ThousandsSeparator: '.', DecimalMark: ','}
	JEP = Currency{Name: "Jersey Pound", IsoCode: "JEP", Symbol: "£", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	JMD = Currency{Name: "Jamaican Dollar", IsoCode: "JMD", Symbol: "$", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	JOD = Currency{Name: "Jordanian Dinar", IsoCode: "JOD", Symbol: "د.ا", SymbolFirst: true, SubunitToUnit: 1000, ThousandsSeparator: ',', DecimalMark: '.'}
	JPY = Currency{Name: "Japanese Yen", IsoCode: "JPY", Symbol: "¥", SymbolFirst: true, SubunitToUnit: 1, ThousandsSeparator: ',', DecimalMark: '.'}
	KES = Currency{Name: "Kenyan Shilling", IsoCode: "KES", Symbol: "KSh", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	KGS = Currency{Name: "Kyrgyzstani Som", IsoCode: "KGS", Symbol: "som", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	KHR = Currency{Name: "Cambodian Riel", IsoCode: "KHR", Symbol: "៛", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	KMF = Currency{Name: "Comorian Franc", IsoCode: "KMF", Symbol: "Fr", SymbolFirst: false, SubunitToUnit: 1, ThousandsSeparator: ',', DecimalMark: '.'}
	KPW = Currency{Name: "North Korean Won", IsoCode: "KPW", Symbol: "₩", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	KRW = Currency{Name: "South Korean Won", IsoCode: "KRW", Symbol: "₩", SymbolFirst: true, SubunitToUnit: 1, ThousandsSeparator: ',', DecimalMark: '.'}
	KWD = Currency{Name: "Kuwaiti Dinar", IsoCode: "KWD", Symbol: "د.ك", SymbolFirst: true, SubunitToUnit: 1000, ThousandsSeparator: ',', DecimalMark: '.'}
	KYD = Currency{Name: "Cayman Islands Dollar", IsoCode: "KYD", Symbol: "$", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	KZT = Currency{Name: "Kazakhstani Tenge", IsoCode: "KZT", Symbol: "₸", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	LAK = Currency{Name: "Lao Kip", IsoCode: "LAK", Symbol: "₭", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	LBP = Currency{Name: "Lebanese Pound", IsoCode: "LBP", Symbol: "ل.ل", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	LKR = Currency{Name: "Sri Lankan Rupee", IsoCode: "LKR", Symbol: "₨", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	LRD = Currency{Name: "Liberian Dollar", IsoCode: "LRD", Symbol: "$", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	LSL = Currency{Name: "Lesotho Loti", IsoCode: "LSL", Symbol: "L", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	LTL = Currency{Name: "Lithuanian Litas", IsoCode: "LTL", Symbol: "Lt", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	LVL = Currency{Name: "Latvian Lats", IsoCode: "LVL", Symbol: "Ls", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	LYD = Currency{Name: "Libyan Dinar", IsoCode: "LYD", Symbol: "ل.د", SymbolFirst: false, SubunitToUnit: 1000, ThousandsSeparator: ',', DecimalMark: '.'}
	MAD = Currency{Name: "Moroccan Dirham", IsoCode: "MAD", Symbol: "د.م.", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	MDL = Currency{Name: "Moldovan Leu", IsoCode: "MDL", Symbol: "L", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	MGA = Currency{Name: "Malagasy Ariary", IsoCode: "MGA", Symbol: "Ar", SymbolFirst: true, SubunitToUnit: 5, ThousandsSeparator: ',', DecimalMark: '.'}
	MKD = Currency{Name: "Macedonian Denar", IsoCode: "MKD", Symbol: "ден", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	MMK = Currency{Name: "Myanmar Kyat", IsoCode: "MMK", Symbol: "K", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	MNT = Currency{Name: "Mongolian Tögrög", IsoCode: "MNT", Symbol: "₮", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	MOP = Currency{Name: "Macanese Pataca", IsoCode: "MOP", Symbol: "P", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	MRO = Currency{Name: "Mauritanian Ouguiya", IsoCode: "MRO", Symbol: "UM", SymbolFirst: false, SubunitToUnit: 5, ThousandsSeparator: ',', DecimalMark: '.'}
	MTL = Currency{Name: "Maltese Lira", IsoCode: "MTL", Symbol: "₤", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	MUR = Currency{Name: "Mauritian Rupee", IsoCode: "MUR", Symbol: "₨", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	MVR = Currency{Name: "Maldivian Rufiyaa", IsoCode: "MVR", Symbol: "MVR", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	MWK = Currency{Name: "Malawian Kwacha", IsoCode: "MWK", Symbol: "MK", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	MXN = Currency{Name: "Mexican Peso", IsoCode: "MXN", Symbol: "$", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	MYR = Currency{Name: "Malaysian Ringgit", IsoCode: "MYR", Symbol: "RM", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	MZN = Currency{Name: "Mozambican Metical", IsoCode: "MZN", Symbol: "MTn", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: '.', DecimalMark: ','}
	NAD = Currency{Name: "Namibian Dollar", IsoCode: "NAD", Symbol: "$", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	NGN = Currency{Name: "Nigerian Naira", IsoCode: "NGN", Symbol: "₦", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	NIO = Currency{Name: "Nicaraguan Córdoba", IsoCode: "NIO", Symbol: "C$", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	NOK = Currency{Name: "Norwegian Krone", IsoCode: "NOK", Symbol: "kr", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: '.', DecimalMark: ','}
	NPR = Currency{Name: "Nepalese Rupee", IsoCode: "NPR", Symbol: "₨", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	NZD = Currency{Name: "New Zealand Dollar", IsoCode: "NZD", Symbol: "$", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	OMR = Currency{Name: "Omani Rial", IsoCode: "OMR", Symbol: "ر.ع.", SymbolFirst: true, SubunitToUnit: 1000, ThousandsSeparator: ',', DecimalMark: '.'}
	PAB = Currency{Name: "Panamanian Balboa", IsoCode: "PAB", Symbol: "B/.", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	PEN = Currency{Name: "Peruvian Sol", IsoCode: "PEN", Symbol: "S/", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	PGK = Currency{Name: "Papua New Guinean Kina", IsoCode: "PGK", Symbol: "K", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	PHP = Currency{Name: "Philippine Peso", IsoCode: "PHP", Symbol: "₱", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	PKR = Currency{Name: "Pakistani Rupee", IsoCode: "PKR", Symbol: "₨", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	PLN = Currency{Name: "Polish Złoty", IsoCode: "PLN", Symbol: "zł", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ' ', DecimalMark: ','}
	PYG = Currency{Name: "Paraguayan Guaraní", IsoCode: "PYG", Symbol: "₲", SymbolFirst: true, SubunitToUnit: 1, ThousandsSeparator: ',', DecimalMark: '.'}
	QAR = Currency{Name: "Qatari Riyal", IsoCode: "QAR", Symbol: "ر.ق", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	RON = Currency{Name: "Romanian Leu", IsoCode: "RON", Symbol: "Lei", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: '.', DecimalMark: ','}
	RSD = Currency{Name: "Serbian Dinar", IsoCode: "RSD", Symbol: "РСД", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	RUB = Currency{Name: "Russian Ruble", IsoCode: "RUB", Symbol: "₽", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: '.', DecimalMark: ','}
	RWF = Currency{Name: "Rwandan Franc", IsoCode: "RWF", Symbol: "FRw", SymbolFirst: false, SubunitToUnit: 1, ThousandsSeparator: ',', DecimalMark: '.'}
	SAR = Currency{Name: "Saudi Riyal", IsoCode: "SAR", Symbol: "ر.س", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	SBD = Currency{Name: "Solomon Islands Dollar", IsoCode: "SBD", Symbol: "$", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	SCR = Currency{Name: "Seychellois Rupee", IsoCode: "SCR", Symbol: "₨", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	SDG = Currency{Name: "Sudanese Pound", IsoCode: "SDG", Symbol: "£", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	SEK = Currency{Name: "Swedish Krona", IsoCode: "SEK", Symbol: "kr", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ' ', DecimalMark: ','}
	SGD = Currency{Name: "Singapore Dollar", IsoCode: "SGD", Symbol: "$", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	SHP = Currency{Name: "Saint Helenian Pound", IsoCode: "SHP", Symbol: "£", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	SKK = Currency{Name: "Slovak Koruna", IsoCode: "SKK", Symbol: "Sk", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	SLL = Currency{Name: "Sierra Leonean Leone", IsoCode: "SLL", Symbol: "Le", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	SOS = Currency{Name: "Somali Shilling", IsoCode: "SOS", Symbol: "Sh", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	SRD = Currency{Name: "Surinamese Dollar", IsoCode: "SRD", Symbol: "$", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	SSP = Currency{Name: "South Sudanese Pound", IsoCode: "SSP", Symbol: "£", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	STD = Currency{Name: "São Tomé and Príncipe Dobra", IsoCode: "STD", Symbol: "Db", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	SVC = Currency{Name: "Salvadoran Colón", IsoCode: "SVC", Symbol: "₡", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	SYP = Currency{Name: "Syrian Pound", IsoCode: "SYP", Symbol: "£S", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	SZL = Currency{Name: "Swazi Lilangeni", IsoCode: "SZL", Symbol: "E", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	THB = Currency{Name: "Thai Baht", IsoCode: "THB", Symbol: "฿", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	TJS = Currency{Name: "Tajikistani Somoni", IsoCode: "TJS", Symbol: "ЅМ", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	TMT = Currency{Name: "Turkmenistani Manat", IsoCode: "TMT", Symbol: "T", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	TND = Currency{Name: "Tunisian Dinar", IsoCode: "TND", Symbol: "د.ت", SymbolFirst: false, SubunitToUnit: 1000, ThousandsSeparator: ',', DecimalMark: '.'}
	TOP = Currency{Name: "Tongan Paʻanga", IsoCode: "TOP", Symbol: "T$", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	TRY = Currency{Name: "Turkish Lira", IsoCode: "TRY", Symbol: "₺", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: '.', DecimalMark: ','}
	TTD = Currency{Name: "Trinidad and Tobago Dollar", IsoCode: "TTD", Symbol: "$", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	TWD = Currency{Name: "New Taiwan Dollar", IsoCode: "TWD", Symbol: "$", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	TZS = Currency{Name: "Tanzanian Shilling", IsoCode: "TZS", Symbol: "Sh", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	UAH = Currency{Name: "Ukrainian Hryvnia", IsoCode: "UAH", Symbol: "₴", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	UGX = Currency{Name: "Ugandan Shilling", IsoCode: "UGX", Symbol: "USh", SymbolFirst: false, SubunitToUnit: 1, ThousandsSeparator: ',', DecimalMark: '.'}
	USD = Currency{Name: "United States Dollar", IsoCode: "USD", Symbol: "$", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	UYU = Currency{Name: "Uruguayan Peso", IsoCode: "UYU", Symbol: "$", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: '.', DecimalMark: ','}
	UZS = Currency{Name: "Uzbekistan Som", IsoCode: "UZS", Symbol: "so'm", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	VEF = Currency{Name: "Venezuelan Bolívar", IsoCode: "VEF", Symbol: "Bs.F", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: '.', DecimalMark: ','}
	VES = Currency{Name: "Venezuelan Bolívar Soberano", IsoCode: "VES", Symbol: "Bs", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: '.', DecimalMark: ','}
	VND = Currency{Name: "Vietnamese Đồng", IsoCode: "VND", Symbol: "₫", SymbolFirst: false, SubunitToUnit: 1, ThousandsSeparator: '.', DecimalMark: ','}
	VUV = Currency{Name: "Vanuatu Vatu", IsoCode: "VUV", Symbol: "Vt", SymbolFirst: true, SubunitToUnit: 1, ThousandsSeparator: ',', DecimalMark: '.'}
	WST = Currency{Name: "Samoan Tala", IsoCode: "WST", Symbol: "T", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	XAF = Currency{Name: "Central African Cfa Franc", IsoCode: "XAF", Symbol: "Fr", SymbolFirst: false, SubunitToUnit: 1, ThousandsSeparator: ',', DecimalMark: '.'}
	XAG = Currency{Name: "Silver (Troy Ounce)", IsoCode: "XAG", Symbol: "oz t", SymbolFirst: false, SubunitToUnit: 1, ThousandsSeparator: ',', DecimalMark: '.'}
	XAU = Currency{Name: "Gold (Troy Ounce)", IsoCode: "XAU", Symbol: "oz t", SymbolFirst: false, SubunitToUnit: 1, ThousandsSeparator: ',', DecimalMark: '.'}
	XBA = Currency{Name: "European Composite Unit", IsoCode: "XBA", Symbol: "", SymbolFirst: false, SubunitToUnit: 1, ThousandsSeparator: ',', DecimalMark: '.'}
	XBB = Currency{Name: "European Monetary Unit", IsoCode: "XBB", Symbol: "", SymbolFirst: false, SubunitToUnit: 1, ThousandsSeparator: ',', DecimalMark: '.'}
	XBC = Currency{Name: "European Unit of Account 9", IsoCode: "XBC", Symbol: "", SymbolFirst: false, SubunitToUnit: 1, ThousandsSeparator: ',', DecimalMark: '.'}
	XBD = Currency{Name: "European Unit of Account 17", IsoCode: "XBD", Symbol: "", SymbolFirst: false, SubunitToUnit: 1, ThousandsSeparator: ',', DecimalMark: '.'}
	XCD = Currency{Name: "East Caribbean Dollar", IsoCode: "XCD", Symbol: "$", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	XDR = Currency{Name: "Special Drawing Rights", IsoCode: "XDR", Symbol: "SDR", SymbolFirst: false, SubunitToUnit: 1, ThousandsSeparator: ',', DecimalMark: '.'}
	XFU = Currency{Name: "UIC Franc", IsoCode: "XFU", Symbol: "", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	XOF = Currency{Name: "West African Cfa Franc", IsoCode: "XOF", Symbol: "Fr", SymbolFirst: false, SubunitToUnit: 1, ThousandsSeparator: ',', DecimalMark: '.'}
	XPD = Currency{Name: "Palladium", IsoCode: "XPD", Symbol: "oz t", SymbolFirst: false, SubunitToUnit: 1, ThousandsSeparator: ',', DecimalMark: '.'}
	XPF = Currency{Name: "Cfp Franc", IsoCode: "XPF", Symbol: "Fr", SymbolFirst: false, SubunitToUnit: 1, ThousandsSeparator: ',', DecimalMark: '.'}
	XPT = Currency{Name: "Platinum", IsoCode: "XPT", Symbol: "oz t", SymbolFirst: false, SubunitToUnit: 1, ThousandsSeparator: ',', DecimalMark: '.'}
	XTS = Currency{Name: "Codes specifically reserved for testing purposes", IsoCode: "XTS", Symbol: "", SymbolFirst: false, SubunitToUnit: 1, ThousandsSeparator: ',', DecimalMark: '.'}
	YER = Currency{Name: "Yemeni Rial", IsoCode: "YER", Symbol: "﷼", SymbolFirst: false, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	ZAR = Currency{Name: "South African Rand", IsoCode: "ZAR", Symbol: "R", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	ZMW = Currency{Name: "Zambian Kwacha", IsoCode: "ZMW", Symbol: "K", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
	ZWL = Currency{Name: "Zimbabwean Dollar", IsoCode: "ZWL", Symbol: "$", SymbolFirst: true, SubunitToUnit: 100, ThousandsSeparator: ',', DecimalMark: '.'}
)
View Source
var AllCurrencies []Currency = []Currency{}/* 182 elements not displayed */

A slice with all pre-defined currencies.

View Source
var DefaultBank, _ = NewBank(AllCurrencies, nil, nil)

The DefaultBank supports all currencies (money.AllCurrencies), but it is unable to exchange currencies (it does not have an exchange rates table). It is helpful to work with currencies if there is no need to exchange them.

Functions

This section is empty.

Types

type Bank

type Bank struct {
	// Map by currency ISO code of all currencies supported by the bank
	Currencies         map[string]Currency
	ExchangeRatesTable ExchangeRatesTable
	// contains filtered or unexported fields
}

A Bank makes it possible to create money. It define in which currencies money can be created and the exchange rates table to exchange monetary value in a currency to another.

func NewBank

func NewBank(currencies []Currency, fetch FetchExchangeRatesTableFunc, cache ExchangeRatesTableCache) (*Bank, error)

NewBank creates a new bank that supports the currencies in the slice currencies and uses fetch to fetch the exchange rates table. If cache is not nil, it is used to set the exchange rates table immediately, while a go routine in background updates the table using fetch. Returns an error if fetch returns error.

func NewBankFromStaticExchangeRatesTable

func NewBankFromStaticExchangeRatesTable(currencies []Currency, table ExchangeRatesTable) (*Bank, error)

NewBankFromStaticExchangeRatesTable is a conveniently function to create a new bank that use a static exchange rates table.

func (*Bank) GetExchangeRate

func (bank *Bank) GetExchangeRate(fromCurrencyIsoCode, toCurrencyIsoCode string) (float64, error)

GetExchangeRate returns the exchange rate to convert the currency with ISO code fromCurrencyIsoCode to the currency with ISO code toCurrencyIsoCode. Returns an error if the bank does not support one of the two currencies or if it does not support the exchange between the two currencies, meaning that the exchange rates table does not have the exchange rate.

func (*Bank) NewMoney

func (bank *Bank) NewMoney(cents int, currencyIsoCode string) (*Money, error)

NewMoney creates a new money of value given in the fractional unit of the given currency. For example, in the US dollar currency the fractional unit is cents, and there are 100 cents in one US dollar. So given the Money representation of one US dollar, the fractional interpretation is 100. Returns error if the currency is not supported by the bank.

Example
package main

import (
	"fmt"

	"github.com/pioz/money"
)

func main() {
	bank, _ := money.NewBankFromStaticExchangeRatesTable([]money.Currency{money.USD}, nil)
	m, _ := bank.NewMoney(100, "USD")
	fmt.Println(m.Format())
}
Output:

$1.00

func (*Bank) NewMoneyFromAmount

func (bank *Bank) NewMoneyFromAmount(amount float64, currencyIsoCode string) (*Money, error)

NewMoney creates a new money of value given in the unit of the given currency. Returns error if the currency is not supported by the bank.

Example
package main

import (
	"fmt"

	"github.com/pioz/money"
)

func main() {
	bank, _ := money.NewBankFromStaticExchangeRatesTable([]money.Currency{money.USD}, nil)
	m, _ := bank.NewMoneyFromAmount(1.502, "USD")
	fmt.Println(m.Format())
}
Output:

$1.50

func (*Bank) UpdateExchangeRatesTable

func (bank *Bank) UpdateExchangeRatesTable() error

UpdateExchangeRatesTable updates the bank exchange rates table by calling the fetch function. If fetch is nil, it has no effect. Returns an error if fetch returns error.

type Currency

type Currency struct {
	// The name of the currency.
	Name string
	// The 3 char identifier of the currency (ISO 4217).
	IsoCode string
	// The symbol of the currency.
	Symbol string
	// Is true if the symbol will be displayed before the amount.
	SymbolFirst bool
	// Number of subunits that compose the unit. For example USD is made of 100
	// cents, so SubunitToUnit is 100.
	SubunitToUnit int
	// Thousands separator.
	ThousandsSeparator rune
	// Decimal mark.
	DecimalMark rune
}

Currency represents a currency.

type ExchangeRates

type ExchangeRates map[string]float64

ExchangeRates is a map of currency ISO code to the exchange rate. See ExchangeRatesTable type for more details.

func (*ExchangeRates) Scan

func (rates *ExchangeRates) Scan(value interface{}) error

Scan scan value into Json, implements sql.Scanner interface.

func (ExchangeRates) Value

func (rates ExchangeRates) Value() (driver.Value, error)

Value return json value, implement driver.Valuer interface.

type ExchangeRatesTable

type ExchangeRatesTable map[string]ExchangeRates

ExchangeRatesTable represents an exchange rates table, a map of currency ISO code to a map of currency ISO code to the exchange rate.

table := map[string]map[string]float64{
  "USD": { "EUR": 0.87, "GBP": 0.74 },
  "EUR": { "USD": 1.16, "GBP": 0.86 },
  "GBP": { "USD": 1.35, "EUR": 1.17 },
}

In this example to convert 1 USD to 1 EUR you have to multiply 1 * 0.87. So to get the exchange rate to convert c1 into c2 you have to access the map with

table[c1][c2]

func (*ExchangeRatesTable) Read

func (table *ExchangeRatesTable) Read(b []byte) (int, error)

Read implements the standard Read interface: fill b from table.

func (*ExchangeRatesTable) Write

func (table *ExchangeRatesTable) Write(b []byte) (int, error)

Write implements the standard Write interface: load table from b.

type ExchangeRatesTableCache

type ExchangeRatesTableCache interface {
	Read() (ExchangeRatesTable, error)
	Write(table ExchangeRatesTable) error
}

ExchangeRatesTableCache is the interface that can be implemented to cache an exchange rates table.

type ExchangeRatesTableFileCache

type ExchangeRatesTableFileCache struct {
	// File path where to store the cache.
	FilePath string
}

ExchangeRatesTableFileCache implements the ExchangeRatesTableCache interface to cache an exchange rates table into a file.

func (ExchangeRatesTableFileCache) Read

Read implements the Read method of ExchangeRatesTableCache interface.

func (ExchangeRatesTableFileCache) Write

Write implements the Write method of ExchangeRatesTableCache interface.

type FetchExchangeRatesTableFunc

type FetchExchangeRatesTableFunc func() (ExchangeRatesTable, error)

FetchExchangeRatesTableFunc is the signature of the function to fetch an exchange rates table. It take no parameters and returns the exchange rates table or an error.

type Money

type Money struct {
	// Factional value of the monetary value.
	Cents int
	// ISO code of the currency of the monetary value.
	Currency string
	// contains filtered or unexported fields
}

Money represents a monetary value in a specific currency.

func NewMoney

func NewMoney(cents int, currencyIsoCode string) (*Money, error)

NewMoney creates a new money of value given in the fractional unit of the given currency using the default bank. Returns error if the currency is not supported by the bank.

Example
package main

import (
	"fmt"

	"github.com/pioz/money"
)

func main() {
	m, _ := money.NewMoney(100, "USD")
	fmt.Println(m.Format())
}
Output:

$1.00

func NewMoneyFromAmount

func NewMoneyFromAmount(amount float64, currencyIsoCode string) (*Money, error)

NewMoney creates a new money of value given in the unit of the given currency using the default bank. Returns error if the currency is not supported by the bank.

Example
package main

import (
	"fmt"

	"github.com/pioz/money"
)

func main() {
	m, _ := money.NewMoneyFromAmount(1.499, "USD")
	fmt.Println(m.Format())
}
Output:

$1.50

func (*Money) Absolute

func (m *Money) Absolute() *Money

Absolute returns the absolute moneraty value of m.

func (*Money) Add

func (m1 *Money) Add(m2 *Money) (*Money, error)

Add returns a new money with monetary value equals to m1 + m2. If m2's currency is different from m1's currency, m2 will be exchanged to m1's currency. Returns error if the bank that generated m1 is different from the bank that generated m2.

func (*Money) Amount

func (m *Money) Amount() float64

Amount returns the numerical value of the money.

Example
package main

import (
	"fmt"

	"github.com/pioz/money"
)

func main() {
	m, _ := money.NewMoney(123, "USD")
	fmt.Println(m.Amount())
}
Output:

1.23

func (*Money) Equals

func (m1 *Money) Equals(m2 *Money) (bool, error)

Equals returns true if m1 and m2 have the same monetaty value. If m2's currency is different from m1's currency, m2 will be exchanged to m1's currency. Returns error if the bank that generated m1 is different from the bank that generated m2.

func (*Money) ExchangeTo

func (m *Money) ExchangeTo(currencyIsoCode string) (*Money, error)

ExchangeTo creates a new money in the currency with ISO code currencyIsoCode converted from m, using the exchange rate value stored in the bank exchange rates table. Returns an error if the currencyIsoCode is not supported by the current bank or if the bank is not able to exchange m.Currency to currencyIsoCode.

Example
package main

import (
	"fmt"

	"github.com/pioz/money"
)

func main() {
	bank, _ := money.NewBankFromStaticExchangeRatesTable([]money.Currency{money.EUR, money.USD}, money.ExchangeRatesTable{
		"EUR": {"USD": 1.154321},
		"USD": {"EUR": 0.86702},
	})
	usd, _ := bank.NewMoney(100, "USD")
	eur, _ := usd.ExchangeTo("EUR")
	fmt.Println(eur.Format())
}
Output:

€0,87

func (*Money) ExchangeToWithRate

func (m *Money) ExchangeToWithRate(currencyIsoCode string, rate float64) (*Money, error)

ExchangeTo creates a new money in the currency with ISO code currencyIsoCode converted from m, using the given exchange rate. Returns an error if the currencyIsoCode is not supported by the current bank.

func (*Money) Format

func (m *Money) Format() string

Format creates a formatted price string according to m's currency fields.

func (*Money) GreaterThan

func (m1 *Money) GreaterThan(m2 *Money) (bool, error)

GreaterThan returns true if m1 is greater than m2. If m2's currency is different from m1's currency, m2 will be exchanged to m1's currency. Returns error if the bank that generated m1 is different from the bank that generated m2.

func (*Money) GreaterThanOrEqual

func (m1 *Money) GreaterThanOrEqual(m2 *Money) (bool, error)

GreaterThan returns true if m1 is greater than or equal to m2. If m2's currency is different from m1's currency, m2 will be exchanged to m1's currency. Returns error if the bank that generated m1 is different from the bank that generated m2.

func (*Money) IsNegative

func (m *Money) IsNegative() bool

IsNegative returns true if m moneraty value is negative.

func (*Money) IsPositive

func (m *Money) IsPositive() bool

IsPositive returns true if m moneraty value is positive.

func (*Money) IsZero

func (m *Money) IsZero() bool

IsZero returns true if m moneraty value is zero.

func (*Money) LessThan

func (m1 *Money) LessThan(m2 *Money) (bool, error)

GreaterThan returns true if m1 is less than m2. If m2's currency is different from m1's currency, m2 will be exchanged to m1's currency. Returns error if the bank that generated m1 is different from the bank that generated m2.

func (*Money) LessThanOrEqual

func (m1 *Money) LessThanOrEqual(m2 *Money) (bool, error)

GreaterThan returns true if m1 is less than or equal to m2. If m2's currency is different from m1's currency, m2 will be exchanged to m1's currency. Returns error if the bank that generated m1 is different from the bank that generated m2.

func (*Money) Multiply

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

Multiply returns a new money with monetary value equals to m1 * mul.

func (*Money) Split

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

Split returns a slice of money with split monetary value in the 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.

Example
package main

import (
	"fmt"

	"github.com/pioz/money"
)

func main() {
	m, _ := money.NewMoney(101, "EUR")
	parts, _ := m.Split(3)
	fmt.Println(parts[0].Format())
	fmt.Println(parts[1].Format())
	fmt.Println(parts[2].Format())
}
Output:

€0,34
€0,34
€0,33

func (*Money) Subtract

func (m1 *Money) Subtract(m2 *Money) (*Money, error)

Subtract returns a new money with monetary value equals to m1 - m2. If m2's currency is different from m1's currency, m2 will be exchanged to m1's currency. Returns error if the bank that generated m1 is different from the bank that generated m2.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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