money

package module
v0.0.0-...-4a41563 Latest Latest
Warning

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

Go to latest
Published: May 17, 2026 License: MIT Imports: 3 Imported by: 0

README

Money

A Go library for working with monetary values.

Under active development — not ready for production use.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCurrencyMismatch = errors.New("money: currency mismatch")
	ErrDivisionByZero   = errors.New("money: division by zero")
	ErrInvalidCurrency  = errors.New("money: invalid currency code")
	ErrInvalidAmount    = errors.New("money: invalid amount")
	ErrAmountOverflow   = errors.New("money: arithmetic overflow")
)

Functions

This section is empty.

Types

type Currency

type Currency uint8

Currency represents an ISO 4217 currency. Use it to inspect currency metadata or pass it between money operations.

const (
	XXX Currency = 0
	XTS Currency = 1
	AED Currency = 2
	AFN Currency = 3
	ALL Currency = 4
	AMD Currency = 5
	AOA Currency = 6
	ARS Currency = 7
	AUD Currency = 8
	AWG Currency = 9
	AZN Currency = 10
	BAM Currency = 11
	BBD Currency = 12
	BDT Currency = 13
	BHD Currency = 14
	BIF Currency = 15
	BMD Currency = 16
	BND Currency = 17
	BOB Currency = 18
	BRL Currency = 19
	BSD Currency = 20
	BTN Currency = 21
	BWP Currency = 22
	BYN Currency = 23
	BZD Currency = 24
	CAD Currency = 25
	CDF Currency = 26
	CHF Currency = 27
	CLP Currency = 28
	CNY Currency = 29
	COP Currency = 30
	CRC Currency = 31
	CUP Currency = 32
	CVE Currency = 33
	CZK Currency = 34
	DJF Currency = 35
	DKK Currency = 36
	DOP Currency = 37
	DZD Currency = 38
	EGP Currency = 39
	ERN Currency = 40
	ETB Currency = 41
	EUR Currency = 42
	FJD Currency = 43
	FKP Currency = 44
	GBP Currency = 45
	GEL Currency = 46
	GHS Currency = 47
	GIP Currency = 48
	GMD Currency = 49
	GNF Currency = 50
	GTQ Currency = 51
	GYD Currency = 52
	HKD Currency = 53
	HNL Currency = 54
	HTG Currency = 55
	HUF Currency = 56
	IDR Currency = 57
	ILS Currency = 58
	INR Currency = 59
	IQD Currency = 60
	IRR Currency = 61
	ISK Currency = 62
	JMD Currency = 63
	JOD Currency = 64
	JPY Currency = 65
	KES Currency = 66
	KGS Currency = 67
	KHR Currency = 68
	KMF Currency = 69
	KPW Currency = 70
	KRW Currency = 71
	KWD Currency = 72
	KYD Currency = 73
	KZT Currency = 74
	LAK Currency = 75
	LBP Currency = 76
	LKR Currency = 77
	LRD Currency = 78
	LSL Currency = 79
	LYD Currency = 80
	MAD Currency = 81
	MDL Currency = 82
	MGA Currency = 83
	MKD Currency = 84
	MMK Currency = 85
	MNT Currency = 86
	MOP Currency = 87
	MRU Currency = 88
	MUR Currency = 89
	MVR Currency = 90
	MWK Currency = 91
	MXN Currency = 92
	MYR Currency = 93
	MZN Currency = 94
	NAD Currency = 95
	NGN Currency = 96
	NIO Currency = 97
	NOK Currency = 98
	NPR Currency = 99
	NZD Currency = 100
	OMR Currency = 101
	PAB Currency = 102
	PEN Currency = 103
	PGK Currency = 104
	PHP Currency = 105
	PKR Currency = 106
	PLN Currency = 107
	PYG Currency = 108
	QAR Currency = 109
	RON Currency = 110
	RSD Currency = 111
	RUB Currency = 112
	RWF Currency = 113
	SAR Currency = 114
	SBD Currency = 115
	SCR Currency = 116
	SDG Currency = 117
	SEK Currency = 118
	SGD Currency = 119
	SHP Currency = 120
	SOS Currency = 121
	SRD Currency = 122
	SSP Currency = 123
	STN Currency = 124
	SYP Currency = 125
	SZL Currency = 126
	THB Currency = 127
	TJS Currency = 128
	TMT Currency = 129
	TND Currency = 130
	TOP Currency = 131
	TRY Currency = 132
	TTD Currency = 133
	TWD Currency = 134
	TZS Currency = 135
	UAH Currency = 136
	UGX Currency = 137
	USD Currency = 138
	UYU Currency = 139
	UZS Currency = 140
	VES Currency = 141
	VND Currency = 142
	VUV Currency = 143
	WST Currency = 144
	XAF Currency = 145
	XCD Currency = 146
	XOF Currency = 147
	XPF Currency = 148
	YER Currency = 149
	ZAR Currency = 150
	ZMW Currency = 151
	ZWG Currency = 152
)

func (Currency) Code

func (c Currency) Code() string

Code returns the ISO 4217 alphabetic code (e.g. "USD", "EUR").

func (Currency) NumericCode

func (c Currency) NumericCode() int

Numeric returns the ISO 4217 numeric code (e.g. 840 for USD, 978 for EUR).

func (Currency) Scale

func (c Currency) Scale() int

Scale returns the number of decimal places used by the currency. For example, USD has a scale of 2 (cents), JPY has a scale of 0.

type Money

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

Money represents a monetary amount in the smallest currency unit (e.g. cents for USD). It is immutable — all operations return a new Money. Use NewMoney to construct from a known cent amount, or Parse to construct from a decimal string.

func New

func New(amount int64, currencyCode string) (Money, error)

New creates an Money from a raw amount in the currency's smallest unit (e.g. cents) and a currency code (e.g. "usd", "eur").

Use this when you already have a cents representation.

func (Money) Add

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

Add returns the sum of two Moneys.

Returns ErrCurrencyMismatch if the Moneys are in different currencies. Returns ErrMoneyOverflow if the result would exceed int64 bounds.

func (Money) Amount

func (m Money) Amount() int64

Amount returns the raw Money in the currency's smallest unit (e.g. cents for USD).

Use this when you need the underlying integer for storage or further processing. For a human-readable representation, use String instead.

func (Money) Compare

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

Compare compares two Moneys and returns -1, 0, or 1.

Returns -1 if lesser, 0 if equal, and 1 if greater. Returns ErrCurrencyMismatch if the Moneys are in different currencies.

func (Money) Currency

func (m Money) Currency() Currency

Currency returns the currency associated with this Money.

func (Money) Equal

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

Equal reports whether two Moneys represent the same monetary amount.

Returns ErrCurrencyMismatch if the Moneys are in different currencies.

func (Money) IsNegative

func (m Money) IsNegative() bool

IsNegative reports whether the Money is less than zero.

func (Money) IsPositive

func (m Money) IsPositive() bool

IsPositive reports whether the amount is greater than zero.

func (Money) IsZero

func (m Money) IsZero() bool

IsZero reports whether the amount is exactly zero.

func (Money) Mul

func (m Money) Mul(factor int64) (Money, error)

Mul returns the Money scaled by an integer factor.

Useful for quantity-based calculations (e.g. 3 items at $5.00 each). Returns ErrMoneyOverflow if the result would exceed int64 bounds.

total, err := price.Mul(3) // 3× the price

func (Money) SameCurrency

func (m Money) SameCurrency(other Money) bool

SameCurrency reports whether two Moneys share the same currency.

This is checked automatically by Add, Sub, Equal, and Compare. Use it directly when you want to validate currencies before a series of operations.

func (Money) Split

func (m Money) Split(n int) ([]Money, error)

func (Money) Sub

func (m Money) Sub(other Money) (Money, error)

Sub returns the difference of two Moneys.

Returns ErrCurrencyMismatch if the Moneys are in different currencies. Returns ErrMoneyOverflow if the result would exceed int64 bounds.

Jump to

Keyboard shortcuts

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