locale

package
v1.57.0 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidMoney  = errors.New("invalid money value")
	ErrNegativeMoney = errors.New("negative money value")
)

Functions

func DetectLocale

func DetectLocale() language.Tag

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

func MustResolve(code string, tag language.Tag) Currency

MustResolve is like Resolve but panics on error.

func Resolve

func Resolve(code string, tag language.Tag) (Currency, error)

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

func ResolveDefault(configured string) (Currency, error)

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

func (c Currency) Code() string

Code returns the ISO 4217 code (e.g. "USD", "EUR").

func (Currency) FormatCents

func (c Currency) FormatCents(cents int64) string

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

func (c Currency) FormatCompactCents(cents int64) string

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

func (c Currency) FormatCompactOptionalCents(cents *int64) string

FormatCompactOptionalCents formats optional cents compactly.

func (Currency) FormatOptionalCents

func (c Currency) FormatOptionalCents(cents *int64) string

FormatOptionalCents formats a *int64 cent value, returning "" for nil.

func (Currency) ParseOptionalCents

func (c Currency) ParseOptionalCents(input string) (*int64, error)

ParseOptionalCents parses an optional money string. Returns (nil, nil) for empty input.

func (Currency) ParseRequiredCents

func (c Currency) ParseRequiredCents(input string) (int64, error)

ParseRequiredCents parses a user-entered money string into cents. Strips the currency symbol if present; bare numbers always accepted.

func (Currency) Symbol

func (c Currency) Symbol() string

Symbol returns the narrow symbol glyph (e.g. "$", "EUR", "GBP", "JPY").

Jump to

Keyboard shortcuts

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