Documentation
¶
Index ¶
- Variables
- type Amount
- func (a Amount) Add(b Amount) Amount
- func (a Amount) IsNegative() bool
- func (a Amount) IsZero() bool
- func (a Amount) MajorFloat() float64
- func (a Amount) MajorString() string
- func (a Amount) MinorInt() int64
- func (a Amount) MulInt(n int64) Amount
- func (a Amount) Neg() Amount
- func (a Amount) SameCurrency(b Amount) bool
- func (a Amount) Sub(b Amount) Amount
- type Currency
- type Formatter
- type FormatterConfig
- type Style
- type View
Constants ¶
This section is empty.
Variables ¶
var ( USD = Currency{Code: "USD", Symbol: "$", Scale: 2} EUR = Currency{Code: "EUR", Symbol: "€", Scale: 2} GBP = Currency{Code: "GBP", Symbol: "£", Scale: 2} CHF = Currency{Code: "CHF", Symbol: "CHF", Scale: 2} CAD = Currency{Code: "CAD", Symbol: "CA$", Scale: 2} JPY = Currency{Code: "JPY", Symbol: "¥", Scale: 0} CNY = Currency{Code: "CNY", Symbol: "¥", Scale: 2} AUD = Currency{Code: "AUD", Symbol: "A$", Scale: 2} NZD = Currency{Code: "NZD", Symbol: "NZ$", Scale: 2} SGD = Currency{Code: "SGD", Symbol: "S$", Scale: 2} SEK = Currency{Code: "SEK", Symbol: "kr", Scale: 2} NOK = Currency{Code: "NOK", Symbol: "kr", Scale: 2} MXN = Currency{Code: "MXN", Symbol: "MX$", Scale: 2} BRL = Currency{Code: "BRL", Symbol: "R$", Scale: 2} INR = Currency{Code: "INR", Symbol: "₹", Scale: 2} HKD = Currency{Code: "HKD", Symbol: "HK$", Scale: 2} KRW = Currency{Code: "KRW", Symbol: "₩", Scale: 0} TRY = Currency{Code: "TRY", Symbol: "₺", Scale: 2} ZAR = Currency{Code: "ZAR", Symbol: "R", Scale: 2} PLN = Currency{Code: "PLN", Symbol: "zł", Scale: 2} RUB = Currency{Code: "RUB", Symbol: "₽", Scale: 2} )
Predefined fiat currencies.
var ( BTC = Currency{Code: "BTC", Symbol: "₿", Scale: 8} ETH = Currency{Code: "ETH", Symbol: "Ξ", Scale: 6} )
Predefined crypto majors.
var ( USDC = Currency{Code: "USDC", Symbol: "USDC", Scale: 6} USDT = Currency{Code: "USDT", Symbol: "USDT", Scale: 6} )
Predefined USD stablecoins (business precision).
var Registry = map[string]Currency{ "USD": USD, "EUR": EUR, "GBP": GBP, "CHF": CHF, "CAD": CAD, "JPY": JPY, "CNY": CNY, "AUD": AUD, "NZD": NZD, "SGD": SGD, "SEK": SEK, "NOK": NOK, "MXN": MXN, "BRL": BRL, "INR": INR, "HKD": HKD, "KRW": KRW, "TRY": TRY, "ZAR": ZAR, "PLN": PLN, "RUB": RUB, "BTC": BTC, "ETH": ETH, "USDC": USDC, "USDT": USDT, }
Registry is a helper map for code-based lookups.
Functions ¶
This section is empty.
Types ¶
type Amount ¶
Amount represents a value in a specific currency.
func FromMajorFloat ¶
FromMajorFloat is a convenience constructor that rounds half away from zero to c.Scale decimal places.
func FromMajorString ¶
FromMajorString parses a major-unit decimal string into an Amount. The string may start with an optional sign and must contain at most c.Scale fractional digits (no grouping separators).
func (Amount) IsNegative ¶
IsNegative reports whether the amount is negative.
func (Amount) MajorFloat ¶
MajorFloat returns a best-effort float64 representation.
func (Amount) MajorString ¶
MajorString returns a normalized signless decimal string using the currency scale.
func (Amount) SameCurrency ¶
SameCurrency reports whether a and b share the same currency code and scale.
type Formatter ¶
type Formatter struct {
// contains filtered or unexported fields
}
Formatter renders amounts according to the config.
func NewFormatter ¶
func NewFormatter(cfg FormatterConfig) Formatter
NewFormatter creates a formatter from the given config.
func USAccounting ¶
func USAccounting() Formatter
USAccounting returns a formatter with accounting negatives.
func (Formatter) FormatCompact ¶
FormatCompact returns a compact representation like "$50k" or "$1.2M".
type FormatterConfig ¶
type FormatterConfig struct {
Style Style
UseGrouping bool
ShowPlus bool
ZeroText string
Locale string
}
FormatterConfig controls formatting behavior.
type View ¶
type View struct {
Minor int64 `json:"minor"`
Major string `json:"major"`
Float float64 `json:"float,omitempty"`
Formatted string `json:"formatted"`
Sign string `json:"sign,omitempty"`
Currency string `json:"currency"`
CurrencySymbol string `json:"currency_symbol"`
}
View represents a formatted perspective on an amount.