numberformat

package
v0.2.8 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package numberformat implements the ECMA-402 Intl.NumberFormat constructor.

locales, _ := locale.ParseList("en-US")
format, _ := numberformat.New(locales, numberformat.Options{})
out := format.Format(numberformat.Int(1234))
_ = out

See README.md for usage examples and SPECS/20-numberformat.md for the contract.

Example

Example demonstrates Intl.NumberFormat.prototype.format with the 0.5 rounding example from ECMA-402 §15.5.

package main

import (
	"fmt"

	gointl "github.com/agentable/go-intl"
	"github.com/agentable/go-intl/locale"
	"github.com/agentable/go-intl/numberformat"
)

func main() {
	format, err := numberformat.New(mustLocaleList("en-US"), numberformat.Options{
		MaximumFractionDigits: gointl.Int(0),
	})
	if err != nil {
		panic(err)
	}

	fmt.Println(format.Format(numberformat.Float(0.5)))

}

func mustLocaleList(tags ...string) locale.List {
	locales, err := locale.ParseList(tags...)
	if err != nil {
		panic(err)
	}
	return locales
}
Output:
1
Example (Options)

Example_options demonstrates the "floor" rounding-mode row from ECMA-402 §15.5.

package main

import (
	"fmt"

	gointl "github.com/agentable/go-intl"
	"github.com/agentable/go-intl/locale"
	"github.com/agentable/go-intl/numberformat"
)

func main() {
	format, err := numberformat.New(mustLocaleList("en-US"), numberformat.Options{
		MaximumFractionDigits: gointl.Int(0),
		RoundingMode:          numberformat.FloorRoundingMode,
	})
	if err != nil {
		panic(err)
	}

	fmt.Println(format.Format(numberformat.Float(-1.5)))

}

func mustLocaleList(tags ...string) locale.List {
	locales, err := locale.ParseList(tags...)
	if err != nil {
		panic(err)
	}
	return locales
}
Output:
-2

Index

Examples

Constants

View Source
const (
	DecimalStyle  Style = "decimal"
	PercentStyle  Style = "percent"
	CurrencyStyle Style = "currency"
	UnitStyle     Style = "unit"

	CurrencyDisplayCode         CurrencyDisplay = "code"
	CurrencyDisplaySymbol       CurrencyDisplay = "symbol"
	CurrencyDisplayNarrowSymbol CurrencyDisplay = "narrowSymbol"
	CurrencyDisplayName         CurrencyDisplay = "name"

	StandardCurrencySign   CurrencySign = "standard"
	AccountingCurrencySign CurrencySign = "accounting"

	ShortUnitDisplay  UnitDisplay = "short"
	NarrowUnitDisplay UnitDisplay = "narrow"
	LongUnitDisplay   UnitDisplay = "long"

	UseGroupingMin2   UseGrouping = "min2"
	UseGroupingAuto   UseGrouping = "auto"
	UseGroupingAlways UseGrouping = "always"
	UseGroupingFalse  UseGrouping = "false"

	StandardNotation    Notation = "standard"
	ScientificNotation  Notation = "scientific"
	EngineeringNotation Notation = "engineering"
	CompactNotation     Notation = "compact"

	ShortCompactDisplay CompactDisplay = "short"
	LongCompactDisplay  CompactDisplay = "long"

	AutoSignDisplay       SignDisplay = "auto"
	AlwaysSignDisplay     SignDisplay = "always"
	ExceptZeroSignDisplay SignDisplay = "exceptZero"
	NegativeSignDisplay   SignDisplay = "negative"
	NeverSignDisplay      SignDisplay = "never"

	CeilRoundingMode       RoundingMode = "ceil"
	FloorRoundingMode      RoundingMode = "floor"
	ExpandRoundingMode     RoundingMode = "expand"
	TruncRoundingMode      RoundingMode = "trunc"
	HalfCeilRoundingMode   RoundingMode = "halfCeil"
	HalfFloorRoundingMode  RoundingMode = "halfFloor"
	HalfExpandRoundingMode RoundingMode = "halfExpand"
	HalfTruncRoundingMode  RoundingMode = "halfTrunc"
	HalfEvenRoundingMode   RoundingMode = "halfEven"

	AutoRoundingPriority          RoundingPriority = "auto"
	MorePrecisionRoundingPriority RoundingPriority = "morePrecision"
	LessPrecisionRoundingPriority RoundingPriority = "lessPrecision"

	AutoTrailingZeroDisplay           TrailingZeroDisplay = "auto"
	StripIfIntegerTrailingZeroDisplay TrailingZeroDisplay = "stripIfInteger"

	LookupLocaleMatcher  LocaleMatcher = "lookup"
	BestFitLocaleMatcher LocaleMatcher = "best fit"
)

Variables

This section is empty.

Functions

func SupportedLocalesOf

func SupportedLocalesOf(locales locale.List, opts Options) (locale.List, error)

Types

type CompactDisplay

type CompactDisplay string

CompactDisplay selects compact notation width. Mirrors Intl.NumberFormat option "compactDisplay".

type Currency

type Currency string

Currency identifies an ISO 4217 currency code. Mirrors Intl.NumberFormat option "currency".

type CurrencyDisplay

type CurrencyDisplay string

CurrencyDisplay selects how currency values are displayed. Mirrors Intl.NumberFormat option "currencyDisplay".

type CurrencySign

type CurrencySign string

CurrencySign selects standard or accounting currency signs. Mirrors Intl.NumberFormat option "currencySign".

type LocaleMatcher

type LocaleMatcher string

LocaleMatcher selects locale negotiation behavior. Mirrors Intl.NumberFormat option "localeMatcher".

type Notation

type Notation string

Notation selects standard, scientific, engineering, or compact notation. Mirrors Intl.NumberFormat option "notation".

type NumberFormat

type NumberFormat struct {
	// contains filtered or unexported fields
}

func New

func New(locales locale.List, opts Options) (*NumberFormat, error)

func (*NumberFormat) Format

func (f *NumberFormat) Format(v Value) string

Format formats a numeric value.

func (*NumberFormat) FormatRange

func (f *NumberFormat) FormatRange(start, end Value) string

FormatRange formats a numeric range.

func (*NumberFormat) FormatRangeToParts

func (f *NumberFormat) FormatRangeToParts(start, end Value) []RangePart

FormatRangeToParts formats a numeric range into ECMA-402 range parts.

func (*NumberFormat) FormatToParts

func (f *NumberFormat) FormatToParts(v Value) []Part

FormatToParts formats a numeric value into ECMA-402 parts.

Example

ExampleNumberFormat_FormatToParts demonstrates Intl.NumberFormat.prototype.formatToParts from ECMA-402.

package main

import (
	"fmt"

	"github.com/agentable/go-intl/locale"
	"github.com/agentable/go-intl/numberformat"
)

func main() {
	format, err := numberformat.New(mustLocaleList("en-US"), numberformat.Options{})
	if err != nil {
		panic(err)
	}

	for _, part := range format.FormatToParts(numberformat.Float(-1.5)) {
		fmt.Printf("%s=%q\n", part.Type, part.Value)
	}

}

func mustLocaleList(tags ...string) locale.List {
	locales, err := locale.ParseList(tags...)
	if err != nil {
		panic(err)
	}
	return locales
}
Output:
minusSign="-"
integer="1"
decimal="."
fraction="5"

func (*NumberFormat) ResolvedOptions

func (f *NumberFormat) ResolvedOptions() ResolvedOptions

type Options

type Options struct {
	Style                    Style
	Currency                 Currency
	CurrencyDisplay          CurrencyDisplay
	CurrencySign             CurrencySign
	Unit                     Unit
	UnitDisplay              UnitDisplay
	MinimumIntegerDigits     *int
	MinimumFractionDigits    *int
	MaximumFractionDigits    *int
	MinimumSignificantDigits *int
	MaximumSignificantDigits *int
	RoundingIncrement        *int
	RoundingPriority         RoundingPriority
	RoundingMode             RoundingMode
	TrailingZeroDisplay      TrailingZeroDisplay
	Notation                 Notation
	CompactDisplay           CompactDisplay
	UseGrouping              UseGrouping
	SignDisplay              SignDisplay
	LocaleMatcher            LocaleMatcher
	NumberingSystem          string
}

type Part

type Part struct {
	Type  PartType `json:"type"`
	Value string   `json:"value"`
}

type PartType

type PartType string
const (
	PartInteger           PartType = "integer"
	PartGroup             PartType = "group"
	PartDecimal           PartType = "decimal"
	PartFraction          PartType = "fraction"
	PartCurrency          PartType = "currency"
	PartPercentSign       PartType = "percentSign"
	PartMinusSign         PartType = "minusSign"
	PartPlusSign          PartType = "plusSign"
	PartNaN               PartType = "nan"
	PartInfinity          PartType = "infinity"
	PartUnit              PartType = "unit"
	PartLiteral           PartType = "literal"
	PartExponentSeparator PartType = "exponentSeparator"
	PartExponentMinusSign PartType = "exponentMinusSign"
	PartExponentInteger   PartType = "exponentInteger"
	PartCompact           PartType = "compact"
	PartApproximatelySign PartType = "approximatelySign"
)

type RangePart

type RangePart struct {
	Type   PartType    `json:"type"`
	Value  string      `json:"value"`
	Source RangeSource `json:"source"`
}

type RangeSource

type RangeSource string
const (
	SourceStartRange RangeSource = "startRange"
	SourceShared     RangeSource = "shared"
	SourceEndRange   RangeSource = "endRange"
)

type ResolvedOptions

type ResolvedOptions struct {
	// Locale is the resolved locale. Mirrors Intl.NumberFormat resolved option "locale".
	Locale locale.Locale `json:"locale"`
	// NumberingSystem is the resolved numbering system. Mirrors Intl.NumberFormat resolved option "numberingSystem".
	NumberingSystem string `json:"numberingSystem"`
	// Style is the resolved presentation style. Mirrors Intl.NumberFormat resolved option "style".
	Style Style `json:"style"`
	// Currency is the resolved currency code. Mirrors Intl.NumberFormat resolved option "currency".
	// Empty when the resolved style is not "currency".
	Currency string `json:"currency,omitempty"`
	// CurrencyDisplay is the resolved currency display. Mirrors Intl.NumberFormat resolved option "currencyDisplay".
	// Empty when the resolved style is not "currency".
	CurrencyDisplay CurrencyDisplay `json:"currencyDisplay,omitempty"`
	// CurrencySign is the resolved currency sign. Mirrors Intl.NumberFormat resolved option "currencySign".
	// Empty when the resolved style is not "currency".
	CurrencySign CurrencySign `json:"currencySign,omitempty"`
	// Unit is the resolved unit identifier. Mirrors Intl.NumberFormat resolved option "unit".
	// Empty when the resolved style is not "unit".
	Unit string `json:"unit,omitempty"`
	// UnitDisplay is the resolved unit display. Mirrors Intl.NumberFormat resolved option "unitDisplay".
	// Empty when the resolved style is not "unit".
	UnitDisplay UnitDisplay `json:"unitDisplay,omitempty"`
	// MinimumIntegerDigits is the resolved minimum integer digits. Mirrors Intl.NumberFormat resolved option "minimumIntegerDigits".
	MinimumIntegerDigits int `json:"minimumIntegerDigits"`
	// MinimumFractionDigits is the resolved minimum fraction digits. Mirrors Intl.NumberFormat resolved option "minimumFractionDigits".
	// Nil when ECMA-402 omits fraction digit properties for significant-digit rounding.
	MinimumFractionDigits *int `json:"minimumFractionDigits,omitempty"`
	// MaximumFractionDigits is the resolved maximum fraction digits. Mirrors Intl.NumberFormat resolved option "maximumFractionDigits".
	// Nil when ECMA-402 omits fraction digit properties for significant-digit rounding.
	MaximumFractionDigits *int `json:"maximumFractionDigits,omitempty"`
	// MinimumSignificantDigits is the resolved minimum significant digits. Mirrors Intl.NumberFormat resolved option "minimumSignificantDigits".
	// Nil when ECMA-402 omits significant digit properties for fraction-digit rounding.
	MinimumSignificantDigits *int `json:"minimumSignificantDigits,omitempty"`
	// MaximumSignificantDigits is the resolved maximum significant digits. Mirrors Intl.NumberFormat resolved option "maximumSignificantDigits".
	// Nil when ECMA-402 omits significant digit properties for fraction-digit rounding.
	MaximumSignificantDigits *int `json:"maximumSignificantDigits,omitempty"`
	// UseGrouping is the resolved grouping behavior. Mirrors Intl.NumberFormat resolved option "useGrouping".
	UseGrouping UseGrouping `json:"useGrouping"`
	// Notation is the resolved notation. Mirrors Intl.NumberFormat resolved option "notation".
	Notation Notation `json:"notation"`
	// CompactDisplay is the resolved compact display. Mirrors Intl.NumberFormat resolved option "compactDisplay".
	// Empty when the resolved notation is not "compact".
	CompactDisplay CompactDisplay `json:"compactDisplay,omitempty"`
	// SignDisplay is the resolved sign display. Mirrors Intl.NumberFormat resolved option "signDisplay".
	SignDisplay SignDisplay `json:"signDisplay"`
	// RoundingIncrement is the resolved rounding increment. Mirrors Intl.NumberFormat resolved option "roundingIncrement".
	RoundingIncrement int `json:"roundingIncrement"`
	// RoundingMode is the resolved rounding mode. Mirrors Intl.NumberFormat resolved option "roundingMode".
	RoundingMode RoundingMode `json:"roundingMode"`
	// RoundingPriority is the resolved rounding priority. Mirrors Intl.NumberFormat resolved option "roundingPriority".
	RoundingPriority RoundingPriority `json:"roundingPriority"`
	// TrailingZeroDisplay is the resolved trailing zero display. Mirrors Intl.NumberFormat resolved option "trailingZeroDisplay".
	TrailingZeroDisplay TrailingZeroDisplay `json:"trailingZeroDisplay"`
}

type RoundingMode

type RoundingMode string

RoundingMode selects the decimal rounding mode. Mirrors Intl.NumberFormat option "roundingMode".

type RoundingPriority

type RoundingPriority string

RoundingPriority selects fraction-vs-significant digit conflict resolution. Mirrors Intl.NumberFormat option "roundingPriority".

type SignDisplay

type SignDisplay string

SignDisplay selects when signs are shown. Mirrors Intl.NumberFormat option "signDisplay".

type Style

type Style string

Style selects the formatter's overall presentation. Mirrors Intl.NumberFormat option "style".

type TrailingZeroDisplay

type TrailingZeroDisplay string

TrailingZeroDisplay selects whether integer zeros are stripped. Mirrors Intl.NumberFormat option "trailingZeroDisplay".

type Unit

type Unit string

Unit identifies a sanctioned unit identifier. Mirrors Intl.NumberFormat option "unit".

type UnitDisplay

type UnitDisplay string

UnitDisplay selects how unit names are displayed. Mirrors Intl.NumberFormat option "unitDisplay".

type UseGrouping

type UseGrouping string

UseGrouping selects integer grouping behavior. Mirrors Intl.NumberFormat option "useGrouping".

type Value

type Value struct {
	// contains filtered or unexported fields
}

Value is an opaque ECMA-402 numeric input for NumberFormat methods.

func BigInt

func BigInt(v *big.Int) Value

BigInt returns an arbitrary-precision integer numeric value. A nil value is treated as zero.

func Decimal

func Decimal(s string) (Value, error)

Decimal parses an ECMA-402 decimal-string bridge value.

func Float

func Float(v float64) Value

Float returns a float64 numeric value, including NaN and infinities.

func Int

func Int(v int64) Value

Int returns a signed integer numeric value.

func Uint

func Uint(v uint64) Value

Uint returns an unsigned integer numeric value.

Jump to

Keyboard shortcuts

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