money

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: AGPL-3.0 Imports: 4 Imported by: 0

Documentation

Overview

Package money (piece ⑩) represents exact amounts of money in a currency.

Amounts are stored as integer minor units (cents) plus an ISO 4217 code, so they are exact, sortable and summable per currency. Arithmetic, conversion and locale formatting use bojanz/currency (decimal, CLDR data).

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalid = errors.New("money: invalid amount")

ErrInvalid is returned for unknown currencies or malformed amounts.

Functions

This section is empty.

Types

type Money

type Money struct {
	Minor    int64  `json:"minor"`
	Currency string `json:"currency"`
}

Money is an exact amount in a currency. The zero value is "no amount".

func MustParse

func MustParse(decimal, code string) Money

MustParse is like Parse but panics on error. Use it in tests.

func New

func New(minor int64, code string) (Money, error)

New returns minor units of the given currency.

func Parse

func Parse(decimal, code string) (Money, error)

Parse parses a decimal string such as "12.99" in the given currency. The value is rounded to the currency's minor units.

func Sum

func Sum(code string, ms ...Money) (Money, error)

Sum adds amounts of the same currency.

func (Money) Add

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

Add returns m + o. Both must share a currency.

func (Money) Amount

func (m Money) Amount() currency.Amount

Amount returns the decimal amount.

func (Money) Convert

func (m Money) Convert(to, rate string) (Money, error)

Convert converts m into currency `to` using rate (units of `to` per unit of m.Currency, as a decimal string). The result is rounded to the minor units of `to`.

func (Money) Decimal

func (m Money) Decimal() string

Decimal returns the amount as a decimal string, e.g. "12.99".

func (Money) Format

func (m Money) Format(locale string) string

Format formats the amount for a locale such as "es" or "en-US" ("12,99 €" / "€12.99").

func (Money) IsZero

func (m Money) IsZero() bool

IsZero reports whether m is the zero value.

func (Money) Scale

func (m Money) Scale(factor string) (Money, error)

Scale returns m multiplied by a decimal factor (e.g. "0.0833333" to normalize a yearly price per month), rounded to minor units.

func (Money) String

func (m Money) String() string

String returns "12.99 EUR".

Jump to

Keyboard shortcuts

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