package number
Import Path
golang.org/x/text/number (on go.dev)
Dependency Relation
imports 6 packages, and imported by one package
Involved Source Files
Package number formats numbers according to the customs of different locales.
The number formats of this package allow for greater formatting flexibility
than passing values to message.Printf calls as is. It currently supports the
builtin Go types and anything that implements the Convert interface
(currently internal).
p := message.NewPrinter(language.English)
p.Printf("%v bottles of beer on the wall.\n", number.Decimal(1234))
// Prints: 1,234 bottles of beer on the wall.
p.Printf("%v of gophers lose too much fur.\n", number.Percent(0.12))
// Prints: 12% of gophers lose too much fur.
p = message.NewPrinter(language.Dutch)
p.Printf("There are %v bikes per household.\n", number.Decimal(1.2))
// Prints: There are 1,2 bikes per household.
The width and scale specified in the formatting directives override the
configuration of the formatter.
format.go
number.go
option.go
Package-Level Type Names (total 3)
A FormatFunc formats a number.
func NewFormat(format FormatFunc, opts ...Option) FormatFunc
func NewFormat(format FormatFunc, opts ...Option) FormatFunc
Digits returns information about which logical digits will be presented to
the user. This information is relevant, for instance, to determine plural
forms.
Format implements format.Formatter. It is for internal use only for now.
Formatter : golang.org/x/text/internal/format.Formatter
Formatter : golang.org/x/text/internal/number.VisibleDigits
func Decimal(x interface{}, opts ...Option) Formatter
func Engineering(x interface{}, opts ...Option) Formatter
func Percent(x interface{}, opts ...Option) Formatter
func PerMille(x interface{}, opts ...Option) Formatter
func Scientific(x interface{}, opts ...Option) Formatter
An Option configures a Formatter.
func FormatWidth(n int) Option
func IncrementString(decimal string) Option
func MaxFractionDigits(max int) Option
func MaxIntegerDigits(max int) Option
func MinFractionDigits(min int) Option
func MinIntegerDigits(min int) Option
func NoSeparator() Option
func Pad(r rune) Option
func PatternOverrides(patterns map[string]string) Option
func Precision(prec int) Option
func Scale(decimals int) Option
func Decimal(x interface{}, opts ...Option) Formatter
func Engineering(x interface{}, opts ...Option) Formatter
func NewFormat(format FormatFunc, opts ...Option) FormatFunc
func Percent(x interface{}, opts ...Option) Formatter
func PerMille(x interface{}, opts ...Option) Formatter
func Scientific(x interface{}, opts ...Option) Formatter
func github.com/nkall/compactnumber.(*Formatter).Format(n int, numOptions ...Option) (string, error)
func github.com/nkall/compactnumber.FormatterAPI.Format(n int, numOptions ...Option) (string, error)
Package-Level Functions (total 17)
Decimal formats a number as a floating point decimal.
Engineering formats a number using engineering notation, which is like
scientific notation, but with the exponent normalized to multiples of 3.
FormatWidth sets the total format width.
IncrementString sets the incremental value to which numbers should be
rounded. For instance: Increment("0.05") will cause 1.44 to round to 1.45.
IncrementString also sets scale to the scale of the increment.
MaxFractionDigits specifies the maximum number of fractional digits.
MaxIntegerDigits limits the number of integer digits, eliminating the
most significant digits.
MinFractionDigits specifies the minimum number of fractional digits.
MinIntegerDigits specifies the minimum number of integer digits, adding
leading zeros when needed.
NewFormat creates a FormatFunc based on another FormatFunc and new options.
Use NewFormat to cash the creation of formatters.
NoSeparator causes a number to be displayed without grouping separators.
Pad sets the rune to be used for filling up to the format width.
PatternOverrides allows users to specify alternative patterns for specific
languages. The Pattern will be overridden for all languages in a subgroup as
well. The function will panic for invalid input. It is best to create this
option at startup time.
PatternOverrides must be the first Option passed to a formatter.
Percent formats a number as a percentage. A value of 1.0 means 100%.
PerMille formats a number as a per mille indication. A value of 1.0 means
1000‰.
Precision sets the maximum number of significant digits. A negative value
means exact.
Scale simultaneously sets MinFractionDigits and MaxFractionDigits to the
given value.
Scientific formats a number in scientific format.
![]() |
The pages are generated with Golds v0.8.2. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |