money

package module
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

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

This section is empty.

Functions

func CompareMoney

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

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

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 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 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).

Types

This section is empty.

Jump to

Keyboard shortcuts

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