Documentation
¶
Index ¶
- Constants
- Variables
- type Amount
- type Currency
- type Formatter
- type Money
- func (m *Money) Absolute() *Money
- func (m *Money) Add(om *Money) (*Money, error)
- func (m *Money) Allocate(rs ...int) ([]*Money, error)
- func (m *Money) Amount() int64
- func (m *Money) AsMajorUnits() float64
- func (m *Money) Currency() *Currency
- func (m *Money) Display() string
- func (m *Money) Equals(om *Money) (bool, error)
- func (m *Money) GreaterThan(om *Money) (bool, error)
- func (m *Money) GreaterThanOrEqual(om *Money) (bool, error)
- func (m *Money) IsNegative() bool
- func (m *Money) IsPositive() bool
- func (m *Money) IsZero() bool
- func (m *Money) LessThan(om *Money) (bool, error)
- func (m *Money) LessThanOrEqual(om *Money) (bool, error)
- func (m Money) MarshalJSON() ([]byte, error)
- func (m *Money) Multiply(mul int64) *Money
- func (m *Money) Negative() *Money
- func (m *Money) Round() *Money
- func (m *Money) SameCurrency(om *Money) bool
- func (m *Money) Split(n int) ([]*Money, error)
- func (m *Money) Subtract(om *Money) (*Money, error)
- func (m *Money) UnmarshalJSON(b []byte) error
Constants ¶
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" 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" BOV = "BOV" COU = "COU" MGA = "MGA" MRU = "MRU" XUA = "XUA" MXV = "MXV" STN = "STN" XSU = "XSU" CHE = "CHE" CHW = "CHW" USN = "USN" UYI = "UYI" UYW = "UYW" VES = "VES" VED = "VED" ZWL = "ZWL" ZWD = "ZWD" XBA = "XBA" XBB = "XBB" XBC = "XBC" XBD = "XBD" XTS = "XTS" XXX = "XXX" XPD = "XPD" XPT = "XPT" )
Constants for active currency codes according to the ISO 4217 standard.
Variables ¶
var ( // UnmarshalJSONFunc is injection point of json.Unmarshaller for money.Money UnmarshalJSON = defaultUnmarshalJSON // MarshalJSONFunc is injection point of json.Marshaller for money.Money MarshalJSON = defaultMarshalJSON // ErrCurrencyMismatch happens when two compared Money don't have the same currency. ErrCurrencyMismatch = errors.New("currencies don't match") )
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) { ... }
var ( // ErrCurrencyNotFound happens when it wasn't possible to find the currency by the given parameter. ErrCurrencyNotFound = errors.New("currency not found") )
Functions ¶
This section is empty.
Types ¶
type Amount ¶
type Amount struct {
// contains filtered or unexported fields
}
Amount is a datastructure that stores the amount being used for calculations.
type Currency ¶
type Currency struct { Code string NumericCode string Fraction int Grapheme string Template string Decimal string Thousand string }
Currency represents money currency information required for formatting.
func AddCurrency ¶ added in v0.2.0
AddCurrency lets you insert or update currency in currencies list.
func CurrencyByNumericCode ¶ added in v1.0.5
CurrencyByNumericCode returns the currency given the numeric code defined in ISO-4271.
func GetCurrency ¶ added in v0.3.5
GetCurrency returns the currency given the code.
type Formatter ¶
type Formatter struct { Fraction int Decimal string Thousand string Grapheme string Template string }
Formatter stores Money formatting information.
func NewFormatter ¶
NewFormatter creates new Formatter instance.
func (*Formatter) Format ¶
Format returns string of formatted integer using given currency template.
func (*Formatter) ToMajorUnits ¶ added in v1.0.5
ToMajorUnits returns float64 representing the value in sub units using the currency data
type Money ¶
type Money struct {
// contains filtered or unexported fields
}
Money represents monetary value information, stores currency and amount value.
func (*Money) Absolute ¶
Absolute returns new Money struct from given Money using absolute monetary value.
func (*Money) Add ¶
Add returns new Money struct with value representing sum of Self and Other Money.
func (*Money) Allocate ¶
Allocate returns slice of Money structs with split Self value in given ratios. It lets split money by given ratios without losing pennies and as Split operations distributes leftover pennies amongst the parties with round-robin principle.
func (*Money) AsMajorUnits ¶ added in v1.0.5
AsMajorUnits lets represent Money struct as subunits (float64) in given Currency value
func (*Money) Display ¶ added in v0.1.0
Display lets represent Money struct as string in given Currency value.
func (*Money) GreaterThan ¶
GreaterThan checks whether the value of Money is greater than the other.
func (*Money) GreaterThanOrEqual ¶
GreaterThanOrEqual checks whether the value of Money is greater or equal than the other.
func (*Money) IsNegative ¶
IsNegative returns boolean of whether the value of Money is negative.
func (*Money) IsPositive ¶
IsPositive returns boolean of whether the value of Money is positive.
func (*Money) LessThanOrEqual ¶
LessThanOrEqual checks whether the value of Money is less or equal than the other.
func (Money) MarshalJSON ¶ added in v1.0.5
MarshalJSON is implementation of json.Marshaller
func (*Money) Multiply ¶
Multiply returns new Money struct with value representing Self multiplied value by multiplier.
func (*Money) Negative ¶
Negative returns new Money struct from given Money using negative monetary value.
func (*Money) SameCurrency ¶
SameCurrency check if given Money is equals by currency.
func (*Money) Split ¶
Split returns slice of Money structs with split Self value in 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.
func (*Money) Subtract ¶
Subtract returns new Money struct with value representing difference of Self and Other Money.
func (*Money) UnmarshalJSON ¶ added in v1.0.5
UnmarshalJSON is implementation of json.Unmarshaller