goney

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2019 License: MIT Imports: 4 Imported by: 0

README

goney

Build Status

Represents monetary amounts in Go with arbitrary-precision arithmetic on their amounts.

Usage

package main

import (
  "fmt"
  "github.com/fedragon/goney"
)

func main () {
	a := goney.FromFloat(goney.EUR, 1.23)

	// Ignoring errors to keep the example concise:
	// don't do this at home!
	b, _ := goney.FromString("EUR 3.45")
	c, _ := a.Add(b)

	fmt.Println(c)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AED = Currency{"AED", 2}

AED United Arab Emirates dirham

View Source
var AFN = Currency{"AFN", 2}

AFN Afghan afghani

View Source
var ALL = Currency{"ALL", 2}

ALL Albanian lek

View Source
var AMD = Currency{"AMD", 2}

AMD Armenian dram

View Source
var ANG = Currency{"ANG", 2}

ANG Netherlands Antillean guilder

View Source
var AOA = Currency{"AOA", 2}

AOA Angolan kwanza

View Source
var ARS = Currency{"ARS", 2}

ARS Argentine peso

View Source
var AUD = Currency{"AUD", 2}

AUD Australian dollar

View Source
var AWG = Currency{"AWG", 2}

AWG Aruban florin

View Source
var AZN = Currency{"AZN", 2}

AZN Azerbaijani manat

View Source
var BAM = Currency{"BAM", 2}

BAM Bosnia and Herzegovina convertible mark

View Source
var BBD = Currency{"BBD", 2}

BBD Barbados dollar

View Source
var BDT = Currency{"BDT", 2}

BDT Bangladeshi taka

View Source
var BGN = Currency{"BGN", 2}

BGN Bulgarian lev

View Source
var BHD = Currency{"BHD", 3}

BHD Bahraini dinar

View Source
var BIF = Currency{"BIF", 0}

BIF Burundian franc

View Source
var BMD = Currency{"BMD", 2}

BMD Bermudian dollar

View Source
var BND = Currency{"BND", 2}

BND Brunei dollar

View Source
var BOB = Currency{"BOB", 2}

BOB Boliviano

View Source
var BOV = Currency{"BOV", 2}

BOV Bolivian Mvdol (funds code)

View Source
var BRL = Currency{"BRL", 2}

BRL Brazilian real

View Source
var BSD = Currency{"BSD", 2}

BSD Bahamian dollar

View Source
var BTN = Currency{"BTN", 2}

BTN Bhutanese ngultrum

View Source
var BWP = Currency{"BWP", 2}

BWP Botswana pula

View Source
var BYN = Currency{"BYN", 2}

BYN Belarusian ruble

View Source
var BZD = Currency{"BZD", 2}

BZD Belize dollar

View Source
var CAD = Currency{"CAD", 2}

CAD Canadian dollar

View Source
var CDF = Currency{"CDF", 2}

CDF Congolese franc

View Source
var CHE = Currency{"CHE", 2}

CHE WIR Euro (complementary currency)

View Source
var CHF = Currency{"CHF", 2}

CHF Swiss franc

View Source
var CHW = Currency{"CHW", 2}

CHW WIR Franc (complementary currency)

View Source
var CLF = Currency{"CLF", 4}

CLF Unidad de Fomento (funds code)

View Source
var CLP = Currency{"CLP", 0}

CLP Chilean peso

View Source
var CNY = Currency{"CNY", 2}

CNY Renminbi (Chinese) yuan

View Source
var COP = Currency{"COP", 2}

COP Colombian peso

View Source
var COU = Currency{"COU", 2}

COU Unidad de Valor Real (UVR) (funds code)

View Source
var CRC = Currency{"CRC", 2}

CRC Costa Rican colon

View Source
var CUC = Currency{"CUC", 2}

CUC Cuban convertible peso

View Source
var CUP = Currency{"CUP", 2}

CUP Cuban peso

View Source
var CVE = Currency{"CVE", 2}

CVE Cape Verdean escudo

View Source
var CZK = Currency{"CZK", 2}

CZK Czech koruna

View Source
var DJF = Currency{"DJF", 0}

DJF Djiboutian franc

View Source
var DKK = Currency{"DKK", 2}

DKK Danish krone

View Source
var DOP = Currency{"DOP", 2}

DOP Dominican peso

View Source
var DZD = Currency{"DZD", 2}

DZD Algerian dinar

View Source
var EGP = Currency{"EGP", 2}

EGP Egyptian pound

View Source
var ERN = Currency{"ERN", 2}

ERN Eritrean nakfa

View Source
var ETB = Currency{"ETB", 2}

ETB Ethiopian birr

View Source
var EUR = Currency{"EUR", 2}

EUR Euro

View Source
var FJD = Currency{"FJD", 2}

FJD Fiji dollar

View Source
var FKP = Currency{"FKP", 2}

FKP Falkland Islands pound

View Source
var GBP = Currency{"GBP", 2}

GBP Pound sterling

View Source
var GEL = Currency{"GEL", 2}

GEL Georgian lari

View Source
var GHS = Currency{"GHS", 2}

GHS Ghanaian cedi

View Source
var GIP = Currency{"GIP", 2}

GIP Gibraltar pound

View Source
var GMD = Currency{"GMD", 2}

GMD Gambian dalasi

View Source
var GNF = Currency{"GNF", 0}

GNF Guinean franc

View Source
var GTQ = Currency{"GTQ", 2}

GTQ Guatemalan quetzal

View Source
var GYD = Currency{"GYD", 2}

GYD Guyanese dollar

View Source
var HKD = Currency{"HKD", 2}

HKD Hong Kong dollar

View Source
var HNL = Currency{"HNL", 2}

HNL Honduran lempira

View Source
var HRK = Currency{"HRK", 2}

HRK Croatian kuna

View Source
var HTG = Currency{"HTG", 2}

HTG Haitian gourde

View Source
var HUF = Currency{"HUF", 2}

HUF Hungarian forint

View Source
var IDR = Currency{"IDR", 2}

IDR Indonesian rupiah

View Source
var ILS = Currency{"ILS", 2}

ILS Israeli new shekel

View Source
var INR = Currency{"INR", 2}

INR Indian rupee

View Source
var IQD = Currency{"IQD", 3}

IQD Iraqi dinar

View Source
var IRR = Currency{"IRR", 2}

IRR Iranian rial

View Source
var ISK = Currency{"ISK", 0}

ISK Icelandic króna

View Source
var JMD = Currency{"JMD", 2}

JMD Jamaican dollar

View Source
var JOD = Currency{"JOD", 3}

JOD Jordanian dinar

View Source
var JPY = Currency{"JPY", 0}

JPY Japanese yen

View Source
var KES = Currency{"KES", 2}

KES Kenyan shilling

View Source
var KGS = Currency{"KGS", 2}

KGS Kyrgyzstani som

View Source
var KHR = Currency{"KHR", 2}

KHR Cambodian riel

View Source
var KMF = Currency{"KMF", 0}

KMF Comoro franc

View Source
var KPW = Currency{"KPW", 2}

KPW North Korean won

View Source
var KRW = Currency{"KRW", 0}

KRW South Korean won

View Source
var KWD = Currency{"KWD", 3}

KWD Kuwaiti dinar

View Source
var KYD = Currency{"KYD", 2}

KYD Cayman Islands dollar

View Source
var KZT = Currency{"KZT", 2}

KZT Kazakhstani tenge

View Source
var LAK = Currency{"LAK", 2}

LAK Lao kip

View Source
var LBP = Currency{"LBP", 2}

LBP Lebanese pound

View Source
var LKR = Currency{"LKR", 2}

LKR Sri Lankan rupee

View Source
var LRD = Currency{"LRD", 2}

LRD Liberian dollar

View Source
var LSL = Currency{"LSL", 2}

LSL Lesotho loti

View Source
var LYD = Currency{"LYD", 3}

LYD Libyan dinar

View Source
var MAD = Currency{"MAD", 2}

MAD Moroccan dirham

View Source
var MDL = Currency{"MDL", 2}

MDL Moldovan leu

View Source
var MGA = Currency{"MGA", 2}

MGA 11] Malagasy ariary

View Source
var MKD = Currency{"MKD", 2}

MKD Macedonian denar

View Source
var MMK = Currency{"MMK", 2}

MMK Myanmar kyat

View Source
var MNT = Currency{"MNT", 2}

MNT Mongolian tögrög

View Source
var MOP = Currency{"MOP", 2}

MOP Macanese pataca

View Source
var MRU = Currency{"MRU", 2}

MRU Mauritanian ouguiya

View Source
var MUR = Currency{"MUR", 2}

MUR Mauritian rupee

View Source
var MVR = Currency{"MVR", 2}

MVR Maldivian rufiyaa

View Source
var MWK = Currency{"MWK", 2}

MWK Malawian kwacha

View Source
var MXN = Currency{"MXN", 2}

MXN Mexican peso

View Source
var MXV = Currency{"MXV", 2}

MXV Mexican Unidad de Inversion (UDI) (funds code)

View Source
var MYR = Currency{"MYR", 2}

MYR Malaysian ringgit

View Source
var MZN = Currency{"MZN", 2}

MZN Mozambican metical

View Source
var NAD = Currency{"NAD", 2}

NAD Namibian dollar

View Source
var NGN = Currency{"NGN", 2}

NGN Nigerian naira

View Source
var NIO = Currency{"NIO", 2}

NIO Nicaraguan córdoba

View Source
var NOK = Currency{"NOK", 2}

NOK Norwegian krone

View Source
var NPR = Currency{"NPR", 2}

NPR Nepalese rupee

View Source
var NZD = Currency{"NZD", 2}

NZD New Zealand dollar

View Source
var OMR = Currency{"OMR", 3}

OMR Omani rial

View Source
var PAB = Currency{"PAB", 2}

PAB Panamanian balboa

View Source
var PEN = Currency{"PEN", 2}

PEN Peruvian sol

View Source
var PGK = Currency{"PGK", 2}

PGK Papua New Guinean kina

View Source
var PHP = Currency{"PHP", 2}

PHP Philippine peso

View Source
var PKR = Currency{"PKR", 2}

PKR Pakistani rupee

View Source
var PLN = Currency{"PLN", 2}

PLN Polish złoty

View Source
var PYG = Currency{"PYG", 0}

PYG Paraguayan guaraní

View Source
var QAR = Currency{"QAR", 2}

QAR Qatari riyal

View Source
var RON = Currency{"RON", 2}

RON Romanian leu

View Source
var RSD = Currency{"RSD", 2}

RSD Serbian dinar

View Source
var RUB = Currency{"RUB", 2}

RUB Russian ruble

View Source
var RWF = Currency{"RWF", 0}

RWF Rwandan franc

View Source
var SAR = Currency{"SAR", 2}

SAR Saudi riyal

View Source
var SBD = Currency{"SBD", 2}

SBD Solomon Islands dollar

View Source
var SCR = Currency{"SCR", 2}

SCR Seychelles rupee

View Source
var SDG = Currency{"SDG", 2}

SDG Sudanese pound

View Source
var SEK = Currency{"SEK", 2}

SEK Swedish krona/kronor

View Source
var SGD = Currency{"SGD", 2}

SGD Singapore dollar

View Source
var SHP = Currency{"SHP", 2}

SHP Saint Helena pound

View Source
var SLL = Currency{"SLL", 2}

SLL Sierra Leonean leone

View Source
var SOS = Currency{"SOS", 2}

SOS Somali shilling

View Source
var SRD = Currency{"SRD", 2}

SRD Surinamese dollar

View Source
var SSP = Currency{"SSP", 2}

SSP South Sudanese pound

View Source
var STN = Currency{"STN", 2}

STN São Tomé and Príncipe dobra

View Source
var SVC = Currency{"SVC", 2}

SVC Salvadoran colón

View Source
var SYP = Currency{"SYP", 2}

SYP Syrian pound

View Source
var SZL = Currency{"SZL", 2}

SZL Swazi lilangeni

View Source
var THB = Currency{"THB", 2}

THB Thai baht

View Source
var TJS = Currency{"TJS", 2}

TJS Tajikistani somoni

View Source
var TMT = Currency{"TMT", 2}

TMT Turkmenistan manat

View Source
var TND = Currency{"TND", 3}

TND Tunisian dinar

View Source
var TOP = Currency{"TOP", 2}

TOP Tongan paʻanga

View Source
var TRY = Currency{"TRY", 2}

TRY Turkish lira

View Source
var TTD = Currency{"TTD", 2}

TTD Trinidad and Tobago dollar

View Source
var TWD = Currency{"TWD", 2}

TWD New Taiwan dollar

View Source
var TZS = Currency{"TZS", 2}

TZS Tanzanian shilling

View Source
var UAH = Currency{"UAH", 2}

UAH Ukrainian hryvnia

View Source
var UGX = Currency{"UGX", 0}

UGX Ugandan shilling

View Source
var USD = Currency{"USD", 2}

USD United States dollar

View Source
var USN = Currency{"USN", 2}

USN United States dollar (next day) (funds code)

View Source
var UYI = Currency{"UYI", 0}

UYI Uruguay Peso en Unidades Indexadas (URUIURUI) (funds code)

View Source
var UYU = Currency{"UYU", 2}

UYU Uruguayan peso

View Source
var UYW = Currency{"UYW", 4}

UYW Unidad previsional[15]

View Source
var UZS = Currency{"UZS", 2}

UZS Uzbekistan som

View Source
var VES = Currency{"VES", 2}

VES Venezuelan bolívar soberano

View Source
var VND = Currency{"VND", 0}

VND Vietnamese đồng

View Source
var VUV = Currency{"VUV", 0}

VUV Vanuatu vatu

View Source
var WST = Currency{"WST", 2}

WST Samoan tala

View Source
var XAF = Currency{"XAF", 0}

XAF CFA franc BEAC

View Source
var XAG = Currency{"XAG", 0}

XAG Silver (one troy

View Source
var XAU = Currency{"XAU", 0}

XAU Gold (one troy

View Source
var XBA = Currency{"XBA", 0}

XBA European Composite Unit (EURCO) (bond market)

View Source
var XBB = Currency{"XBB", 0}

XBB European Monetary Unit (E.M.U.-6) (bond market)

View Source
var XBC = Currency{"XBC", 0}

XBC European Unit of Account 9 (E.U.A.-9) (bond market)

View Source
var XBD = Currency{"XBD", 0}

XBD European Unit of Account 17 (E.U.A.-17) (bond market)

View Source
var XCD = Currency{"XCD", 2}

XCD East Caribbean dollar

View Source
var XDR = Currency{"XDR", 0}

XDR Special drawing rights

View Source
var XOF = Currency{"XOF", 0}

XOF CFA franc BCEAO

View Source
var XPD = Currency{"XPD", 0}

XPD Palladium

View Source
var XPF = Currency{"XPF", 0}

XPF CFP franc

View Source
var XPT = Currency{"XPT", 0}

XPT Platinum

View Source
var XSU = Currency{"XSU", 0}

XSU SUCRE

View Source
var XTS = Currency{"XTS", 0}

XTS Code reserved for testing

View Source
var XUA = Currency{"XUA", 0}

XUA ADB Unit of Account

View Source
var XXX = Currency{"XXX", 0}

XXX No currency

View Source
var YER = Currency{"YER", 2}

YER Yemeni rial

View Source
var ZAR = Currency{"ZAR", 2}

ZAR South African rand

View Source
var ZMW = Currency{"ZMW", 2}

ZMW Zambian kwacha

View Source
var ZWL = Currency{"ZWL", 2}

ZWL Zimbabwean dollar

Functions

This section is empty.

Types

type Currency

type Currency struct {
	Code      string
	Precision int32
}

Currency represents an ISO4217 currency

func Find

func Find(code string) Currency

Find returns the currency having provided code, if found, or XXX (= no currency) if not found

func (Currency) String

func (c Currency) String() string

type Money

type Money struct {
	Amount   decimal.Decimal
	Currency Currency
}

Money represents a monetary amount

func FromFloat

func FromFloat(currency Currency, amount float64) Money

FromFloat returns a Money instance representing the provided amount and currency

func FromInt

func FromInt(currency Currency, amount int64) Money

FromInt returns a Money instance representing the provided amount and currency

func FromString

func FromString(value string) (Money, error)

FromString returns a Money instance representing the provided amount and currency. It assumes that the input string will have the form: <currency code> <amount> e.g. EUR 1.234

func (Money) Add

func (m Money) Add(x Money) (Money, error)

Add returns a Money whose amount is the sum of the amounts in this and the other Money instance, if their currencies match. It returns an error if their currencies do not match

func (Money) Compare

func (m Money) Compare(x Money) (int, error)

Compare compares two Money instances and returns -1, 0, 1 if the first amount is respectively lower than, equal to, or greater than the second one. It returns an error if their currencies don't match.

func (Money) Divide

func (m Money) Divide(x decimal.Decimal) (Money, error)

Divide returns a Money whose amount is the division of the amount in this instance by the provided dividend. It returns an error if the dividend is zero.

func (Money) Equal

func (m Money) Equal(x Money) (bool, error)

Equal compares two Money instances for deep equality, returning true if their amounts and currencies are themselves equal

func (Money) FormattedString

func (m Money) FormattedString() string

FormattedString returns a string representation of this instance, rounding its amount according to the currency's own precision

func (Money) Multiply

func (m Money) Multiply(x decimal.Decimal) Money

Multiply returns a Money whose amount is the multiplication of the amount in this instance by the provided multiplier

func (Money) String

func (m Money) String() string

func (Money) Subtract

func (m Money) Subtract(x Money) (Money, error)

Subtract returns a Money whose amount is the difference of the amounts in this and the other Money instance, if their currencies match. It returns an error if their currencies do not match

Jump to

Keyboard shortcuts

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