money

package module
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: May 6, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package money — ISO 4217 currency precision lookup and strict conversion helpers. The conversions in money.go take an explicit `decimals` parameter; in many callers the decimals are determined solely by the currency code, and a strict check that the wire money matches an expected currency avoids silent currency coercion bugs.

Package money provides conversions between google.type.Money protobuf messages and decimalx.Decimal values.

Index

Constants

View Source
const CentsPerUnit = 100

CentsPerUnit is the number of cents in one currency unit.

View Source
const NanosPerCent = 10_000_000

NanosPerCent is the number of nanos in one cent.

Variables

View Source
var ErrCurrencyMismatch = errors.New("money: currency mismatch")

ErrCurrencyMismatch is returned when a *money.Money's currency code does not match the expected currency. Callers should never silently coerce money between currencies; converting an off-currency amount without explicit FX is a correctness bug.

View Source
var ErrNilMoney = errors.New("money: nil input")

ErrNilMoney is returned when a nil *money.Money is passed to a strict converter that requires a non-nil input.

View Source
var ErrSignMismatch = errors.New("money: units and nanos have opposite signs")

ErrSignMismatch is returned when a *money.Money has units and nanos with opposite signs, which is invalid per google.type.Money semantics.

Functions

func CompareMoney

func CompareMoney(a, b *money.Money) int

CompareMoney compares two Money values numerically, returning -1, 0, or 1.

func Decimals added in v0.8.0

func Decimals(currencyCode string) int32

Decimals returns the ISO 4217 minor-unit count (0, 2, or 3) for the supplied currency code. The lookup is case-insensitive. Unknown or empty codes default to 2 — the most common precision and the safest fallback for codes outside the published list.

func FromFloat64

func FromFloat64(currency string, amount float64) *money.Money

FromFloat64 converts a float64 amount and currency code into a google.type.Money.

func FromInt64

func FromInt64(currency string, amount int64, decimals int32) *money.Money

FromInt64 converts an int64 amount in the smallest unit to a google.type.Money. Shorthand for FromSmallestUnit.

func FromMinorUnitsByCurrency added in v0.8.0

func FromMinorUnitsByCurrency(currency string, minor int64) *money.Money

FromMinorUnitsByCurrency converts an int64 minor-unit amount and a currency code to a *money.Money, looking up the ISO 4217 precision for the code. The returned message has the supplied currency stamped on it; if the currency is unknown the conversion uses 2 decimals (the Decimals fallback).

func FromMoney

func FromMoney(m *money.Money) decimalx.Decimal

FromMoney converts a google.type.Money protobuf message back to a Decimal.

func FromSmallestUnit

func FromSmallestUnit(currency string, amount int64, decimals int32) *money.Money

FromSmallestUnit converts a smallest-unit integer back to a google.type.Money. For example, 1500000000000000000 wei with decimals=18 and currency "ETH" returns Money{Units: 1, Nanos: 500000000}.

func FromSmallestUnitDecimal

func FromSmallestUnitDecimal(currency string, amount decimalx.Decimal, decimals int32) *money.Money

FromSmallestUnitDecimal converts a Decimal in the smallest unit back to a google.type.Money. Use this when the smallest-unit value may exceed int64 range.

func ToCents

func ToCents(units int64, nanos int32) int64

ToCents converts units and nanos to the smallest currency unit (cents).

func ToFloat64

func ToFloat64(m *money.Money) float64

ToFloat64 converts a google.type.Money to a float64.

func ToInt64

func ToInt64(m *money.Money, decimals int32) int64

ToInt64 converts a google.type.Money to an int64 in the smallest unit. Shorthand for ToSmallestUnit.

func ToMinorUnitsByCurrency added in v0.8.0

func ToMinorUnitsByCurrency(m *money.Money, expectedCurrency string) (int64, error)

ToMinorUnitsByCurrency is a shortcut combining Decimals + ToSmallestUnitStrict: it looks up the ISO 4217 precision for expectedCurrency, validates currency match, and converts. Use it when the caller wants currency-aware precision without juggling the decimals argument.

func ToMoney

func ToMoney(currency string, amount decimalx.Decimal) *money.Money

ToMoney converts a Decimal to a google.type.Money protobuf message. Units holds the integer part; Nanos holds the fractional part scaled to 10^9.

func ToSmallestUnit

func ToSmallestUnit(m *money.Money, decimals int32) int64

ToSmallestUnit converts a google.type.Money to its smallest unit representation given the number of decimal places for the currency. For example, 1.5 ETH with decimals=18 returns 1500000000000000000 (wei).

func ToSmallestUnitDecimal

func ToSmallestUnitDecimal(m *money.Money, decimals int32) decimalx.Decimal

ToSmallestUnitDecimal converts a google.type.Money to a Decimal representing the amount in the smallest unit. This avoids int64 overflow for very large values (e.g. wei amounts exceeding MaxInt64).

func ToSmallestUnitStrict added in v0.8.0

func ToSmallestUnitStrict(m *money.Money, expectedCurrency string, decimals int32) (int64, error)

ToSmallestUnitStrict converts a *money.Money to int64 minor units, validating that the currency matches expectedCurrency (case-insensitive) and that the units/nanos signs agree. It is the strict variant of ToSmallestUnit for callers that need to refuse silent coercion.

Types

This section is empty.

Jump to

Keyboard shortcuts

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