Documentation
¶
Index ¶
- Variables
- func DetectLocale() language.Tag
- type Currency
- func (c Currency) Code() string
- func (c Currency) FormatCents(cents int64) string
- func (c Currency) FormatCompactCents(cents int64) string
- func (c Currency) FormatCompactOptionalCents(cents *int64) string
- func (c Currency) FormatOptionalCents(cents *int64) string
- func (c Currency) ParseOptionalCents(input string) (*int64, error)
- func (c Currency) ParseRequiredCents(input string) (int64, error)
- func (c Currency) Symbol() string
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidMoney = errors.New("invalid money value") ErrNegativeMoney = errors.New("negative money value") )
Functions ¶
func DetectLocale ¶
DetectLocale reads the user's formatting locale from the environment. Checks LC_MONETARY, LC_ALL, then LANG. Falls back to American English.
Types ¶
type Currency ¶
type Currency struct {
// contains filtered or unexported fields
}
Currency holds resolved currency formatting state. The currency unit (what money) and formatting locale (how to display numbers) are independent concerns -- like a timestamp (UTC) and a timezone (display).
Safe for concurrent read access; treat as immutable after creation.
func DefaultCurrency ¶
func DefaultCurrency() Currency
DefaultCurrency returns USD with standard US English formatting.
func MustResolve ¶
MustResolve is like Resolve but panics on error.
func Resolve ¶
Resolve creates a Currency from an ISO 4217 code and a formatting locale. The code determines the currency unit and symbol; the tag determines number grouping, decimal separator, and symbol placement.
func ResolveDefault ¶
ResolveDefault resolves the currency code using the config layering: explicit code > MICASA_CURRENCY env > LC_MONETARY/LANG auto-detect > USD. The formatting locale is always detected from the environment.
func (Currency) FormatCents ¶
FormatCents formats an int64 cent value as a locale-appropriate currency string. Uses the locale's number grouping and decimal separator, with the currency symbol placed per locale convention (no extra space).
func (Currency) FormatCompactCents ¶
FormatCompactCents formats cents using abbreviated notation for large values (e.g. 1.2k, 45k, 1.3M) with the correct currency symbol. Values under 1,000 in the base unit use full precision.
func (Currency) FormatCompactOptionalCents ¶
FormatCompactOptionalCents formats optional cents compactly.
func (Currency) FormatOptionalCents ¶
FormatOptionalCents formats a *int64 cent value, returning "" for nil.
func (Currency) ParseOptionalCents ¶
ParseOptionalCents parses an optional money string. Returns (nil, nil) for empty input.
func (Currency) ParseRequiredCents ¶
ParseRequiredCents parses a user-entered money string into cents. Strips the currency symbol if present; bare numbers always accepted.