money

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2023 License: MIT Imports: 6 Imported by: 0

README

Money

What's new this this fork?

Use decimal to store the value of amount

Documentation

Index

Constants

View Source
const (
	AED = "AED"
	AFN = "AFN"
	ALL = "ALL"
	AMD = "AMD"
	ANG = "ANG"
	AOA = "AOA"
	ARS = "ARS"
	AUD = "AUD"
	AWG = "AWG"
	AZN = "AZN"
	BAM = "BAM"
	BBD = "BBD"
	BDT = "BDT"
	BGN = "BGN"
	BHD = "BHD"
	BIF = "BIF"
	BMD = "BMD"
	BND = "BND"
	BOB = "BOB"
	BRL = "BRL"
	BSD = "BSD"
	BTN = "BTN"
	BWP = "BWP"
	BYN = "BYN"
	BYR = "BYR"
	BZD = "BZD"
	CAD = "CAD"
	CDF = "CDF"
	CHF = "CHF"
	CLF = "CLF"
	CLP = "CLP"
	CNY = "CNY"
	COP = "COP"
	CRC = "CRC"
	CUC = "CUC"
	CUP = "CUP"
	CVE = "CVE"
	CZK = "CZK"
	DJF = "DJF"
	DKK = "DKK"
	DOP = "DOP"
	DZD = "DZD"
	EEK = "EEK"
	EGP = "EGP"
	ERN = "ERN"
	ETB = "ETB"
	EUR = "EUR"
	FJD = "FJD"
	FKP = "FKP"
	GBP = "GBP"
	GEL = "GEL"
	GGP = "GGP"
	GHC = "GHC"
	GHS = "GHS"
	GIP = "GIP"
	GMD = "GMD"
	GNF = "GNF"
	GTQ = "GTQ"
	GYD = "GYD"
	HKD = "HKD"
	HNL = "HNL"
	HRK = "HRK"
	HTG = "HTG"
	HUF = "HUF"
	IDR = "IDR"
	ILS = "ILS"
	IMP = "IMP"
	INR = "INR"
	IQD = "IQD"
	IRR = "IRR"
	ISK = "ISK"
	JEP = "JEP"
	JMD = "JMD"
	JOD = "JOD"
	JPY = "JPY"
	KES = "KES"
	KGS = "KGS"
	KHR = "KHR"
	KMF = "KMF"
	KPW = "KPW"
	KRW = "KRW"
	KWD = "KWD"
	KYD = "KYD"
	KZT = "KZT"
	LAK = "LAK"
	LBP = "LBP"
	LKR = "LKR"
	LRD = "LRD"
	LSL = "LSL"
	LTL = "LTL"
	LVL = "LVL"
	LYD = "LYD"
	MAD = "MAD"
	MDL = "MDL"
	MGA = "MGA"
	MKD = "MKD"
	MMK = "MMK"
	MNT = "MNT"
	MOP = "MOP"
	MUR = "MUR"
	MVR = "MVR"
	MWK = "MWK"
	MXN = "MXN"
	MYR = "MYR"
	MZN = "MZN"
	NAD = "NAD"
	NGN = "NGN"
	NIO = "NIO"
	NOK = "NOK"
	NPR = "NPR"
	NZD = "NZD"
	OMR = "OMR"
	PAB = "PAB"
	PEN = "PEN"
	PGK = "PGK"
	PHP = "PHP"
	PKR = "PKR"
	PLN = "PLN"
	PYG = "PYG"
	QAR = "QAR"
	RON = "RON"
	RSD = "RSD"
	RUB = "RUB"
	RUR = "RUR"
	RWF = "RWF"
	SAR = "SAR"
	SBD = "SBD"
	SCR = "SCR"
	SDG = "SDG"
	SEK = "SEK"
	SGD = "SGD"
	SHP = "SHP"
	SKK = "SKK"
	SLL = "SLL"
	SOS = "SOS"
	SRD = "SRD"
	SSP = "SSP"
	STD = "STD"
	SVC = "SVC"
	SYP = "SYP"
	SZL = "SZL"
	THB = "THB"
	TJS = "TJS"
	TMT = "TMT"
	TND = "TND"
	TOP = "TOP"
	TRL = "TRL"
	TRY = "TRY"
	TTD = "TTD"
	TWD = "TWD"
	TZS = "TZS"
	UAH = "UAH"
	UGX = "UGX"
	USD = "USD"
	UYU = "UYU"
	UZS = "UZS"
	VEF = "VEF"
	VND = "VND"
	VUV = "VUV"
	WST = "WST"
	XAF = "XAF"
	XAG = "XAG"
	XAU = "XAU"
	XCD = "XCD"
	XDR = "XDR"
	XOF = "XOF"
	XPF = "XPF"
	YER = "YER"
	ZAR = "ZAR"
	ZMW = "ZMW"
	ZWD = "ZWD"
	ZWL = "ZWL"
)

Constants for active currency codes according to the ISO 4217 standard.

Variables

View Source
var (
	// ErrCurrencyMismatch happens when two compared Money don't have the same currency.
	ErrCurrencyMismatch = errors.New("currencies don't match")

	// ErrInvalidJSONUnmarshal happens when the default money.UnmarshalJSON fails to unmarshal Money because of invalid data.
	ErrInvalidJSONUnmarshal = errors.New("invalid json unmarshal")
)

Injection points for backward compatibility. If you need to keep your JSON marshal/unmarshal way, overwrite them like below.

money.UnmarshalJSON = func (m *Money, b []byte) error { ... }
money.MarshalJSON = func (m Money) ([]byte, error) { ... }

Functions

This section is empty.

Types

type Amount

type Amount = decimal.Decimal

Amount is a data structure that stores the amount being used for calculations.

type Currencies

type Currencies map[string]*Currency

func (Currencies) Add

func (c Currencies) Add(currency *Currency) Currencies

Add updates currencies list by adding a given Currency to it.

func (Currencies) CurrencyByCode

func (c Currencies) CurrencyByCode(code string) *Currency

CurrencyByCode returns the currency given the currency code defined as a constant.

func (Currencies) CurrencyByNumericCode

func (c Currencies) CurrencyByNumericCode(code string) *Currency

CurrencyByNumericCode returns the currency given the numeric code defined in ISO-4271.

type Currency

type Currency struct {
	Code        string `json:"code,omitempty"`
	NumericCode string `json:"numeric_code,omitempty"`
	Fraction    int    `json:"fraction,omitempty"`
	Grapheme    string `json:"grapheme,omitempty"`
	Template    string `json:"template,omitempty"`
	Decimal     string `json:"decimal,omitempty"`
	Thousand    string `json:"thousand,omitempty"`
}

Currency represents money currency information required for formatting. Only for currency info and display propose

func AddCurrency

func AddCurrency(code, Grapheme, Template, Decimal, Thousand string, Fraction int) *Currency

AddCurrency lets you insert or update currency in currencies list.

func GetCurrency

func GetCurrency(code string) *Currency

GetCurrency returns the currency given the code.

func (*Currency) Formatter

func (c *Currency) Formatter() *Formatter

Formatter returns currency formatter representing used currency structure.

type Formatter

type Formatter struct {
	Fraction int
	Decimal  string
	Thousand string
	Grapheme string
	Template string
}

Formatter stores Money formatting information.

func NewFormatter

func NewFormatter(fraction int, decimal, thousand, grapheme, template string) *Formatter

NewFormatter creates new Formatter instance.

func (*Formatter) Format

func (f *Formatter) Format(amount decimal.Decimal) string

Format returns string of formatted integer using given currency template.

type Money

type Money struct {
	// contains filtered or unexported fields
}

Money represents monetary value information, stores currency and amount value.

func New

func New(amount decimal.Decimal, code string) *Money

New creates and returns new instance of Money.

func NewFromFloat added in v0.0.3

func NewFromFloat(amount float64, code string) *Money

New creates and returns new instance of Money.

func (*Money) Add

func (m *Money) Add(a ...*Money) (*Money, error)

func (*Money) Convert added in v0.0.4

func (m *Money) Convert(currencyCode string, exchangeRate *float64) *Money

func (*Money) Currency

func (m *Money) Currency() *Currency

Currency returns the currency used by Money.

func (*Money) Display

func (m *Money) Display() string

Display lets represent Money struct as string in given Currency value.

func (*Money) GetAmount added in v0.0.2

func (m *Money) GetAmount() *Amount

func (Money) MarshalJSON added in v0.0.5

func (m Money) MarshalJSON() ([]byte, error)

MarshalJSON marshals the Money struct to JSON.

func (*Money) UnmarshalJSON added in v0.0.5

func (m *Money) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals the Money struct from JSON.

Jump to

Keyboard shortcuts

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