Documentation
¶
Overview ¶
Package money provides conversions between google.type.Money protobuf messages and decimalx.Decimal values.
Index ¶
- Constants
- func CompareMoney(a, b *money.Money) int
- func FromFloat64(currency string, amount float64) *money.Money
- func FromInt64(currency string, amount int64, decimals int32) *money.Money
- func FromMoney(m *money.Money) decimalx.Decimal
- func FromSmallestUnit(currency string, amount int64, decimals int32) *money.Money
- func FromSmallestUnitDecimal(currency string, amount decimalx.Decimal, decimals int32) *money.Money
- func ToCents(units int64, nanos int32) int64
- func ToFloat64(m *money.Money) float64
- func ToInt64(m *money.Money, decimals int32) int64
- func ToMoney(currency string, amount decimalx.Decimal) *money.Money
- func ToSmallestUnit(m *money.Money, decimals int32) int64
- func ToSmallestUnitDecimal(m *money.Money, decimals int32) decimalx.Decimal
Constants ¶
const CentsPerUnit = 100
CentsPerUnit is the number of cents in one currency unit.
const NanosPerCent = 10_000_000
NanosPerCent is the number of nanos in one cent.
Variables ¶
This section is empty.
Functions ¶
func CompareMoney ¶
CompareMoney compares two Money values numerically, returning -1, 0, or 1.
func FromFloat64 ¶
FromFloat64 converts a float64 amount and currency code into a google.type.Money.
func FromInt64 ¶
FromInt64 converts an int64 amount in the smallest unit to a google.type.Money. Shorthand for FromSmallestUnit.
func FromSmallestUnit ¶
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 ¶
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 ToInt64 ¶
ToInt64 converts a google.type.Money to an int64 in the smallest unit. Shorthand for ToSmallestUnit.
func ToMoney ¶
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 ¶
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 ¶
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.