package number

Import Path
	golang.org/x/text/internal/number (on go.dev)

Dependency Relation
	imports 7 packages, and imported by 3 packages

Involved Source Files common.go decimal.go format.go Package number contains tools and data for formatting numbers. pattern.go roundingmode_string.go tables.go
Package-Level Type Names (total 11)
/* sort by: | */
A Converter converts a number into decimals according to the given rounding criteria. ( Converter) Convert(d *Decimal, r RoundingContext)
A Decimal represents a floating point number in decimal format. Digits represents a number [0, 1.0), and the absolute value represented by Decimal is Digits * 10^Exp. Leading and trailing zeros may be omitted and Exp may point outside a valid position in Digits. Examples: Number Decimal 12345 Digits: [1, 2, 3, 4, 5], Exp: 5 12.345 Digits: [1, 2, 3, 4, 5], Exp: 2 12000 Digits: [1, 2], Exp: 5 12000.00 Digits: [1, 2], Exp: 5 0.00123 Digits: [1, 2, 3], Exp: -2 0 Digits: [], Exp: 0 // mantissa digits, big-endian // exponent // Takes precedence over Digits and Exp. // Takes precedence over Inf. digits.Neg bool Convert converts the given number to the decimal representation using the supplied RoundingContext. ConvertFloat converts a floating point number to decimals. ConvertInt converts an integer to decimals. (*Decimal) String() string *Decimal : expvar.Var *Decimal : fmt.Stringer func FormatDigits(d *Decimal, r RoundingContext) Digits func Converter.Convert(d *Decimal, r RoundingContext) func (*Formatter).Format(dst []byte, d *Decimal) []byte
Digits represents a floating point number represented in digits of the base in which a number is to be displayed. It is similar to Decimal, but keeps track of trailing fraction zeros and the comma placement for engineering notation. Digits must have at least one digit. Examples: Number Decimal decimal 12345 Digits: [1, 2, 3, 4, 5], Exp: 5 End: 5 12.345 Digits: [1, 2, 3, 4, 5], Exp: 2 End: 5 12000 Digits: [1, 2], Exp: 5 End: 5 12000.00 Digits: [1, 2], Exp: 5 End: 7 0.00123 Digits: [1, 2, 3], Exp: -2 End: 3 0 Digits: [], Exp: 0 End: 1 scientific (actual exp is Exp - Comma) 0e0 Digits: [0], Exp: 1, End: 1, Comma: 1 .0e0 Digits: [0], Exp: 0, End: 1, Comma: 0 0.0e0 Digits: [0], Exp: 1, End: 2, Comma: 1 1.23e4 Digits: [1, 2, 3], Exp: 5, End: 3, Comma: 1 .123e5 Digits: [1, 2, 3], Exp: 5, End: 3, Comma: 0 engineering 12.3e3 Digits: [1, 2, 3], Exp: 5, End: 3, Comma: 2 Comma is used for the comma position for scientific (always 0 or 1) and engineering notation (always 0, 1, 2, or 3). // mantissa digits, big-endian // exponent // Takes precedence over Digits and Exp. // Takes precedence over Inf. digits.Neg bool End indicates the end position of the number. // For decimals Exp <= End. For scientific len(Digits) <= End. IsScientific indicates whether this number is to be rendered as a scientific number. (*Digits) NumFracDigits() int func FormatDigits(d *Decimal, r RoundingContext) Digits func VisibleDigits.Digits(buf []byte, t language.Tag, scale int) Digits func golang.org/x/text/number.Formatter.Digits(buf []byte, tag language.Tag, scale int) Digits func (*Formatter).Render(dst []byte, d Digits) []byte
Formatter contains all the information needed to render a number. Info Info Pattern Pattern // includes prefix and suffix. First byte is prefix length. Pattern.Flags PatternFlag Pattern.FormatWidth uint16 Pattern.GroupingSize [2]uint8 // Offset into Affix for negative prefix and suffix or 0. // Offset into Affix for prefix and suffix Pattern.PadRune rune Pattern.RoundingContext RoundingContext // Number of decimals to shift. Used for % and ‰. Pattern.RoundingContext.Increment uint32 // May differ from printed scale. // -1 is unlimited Pattern.RoundingContext.MaxIntegerDigits uint8 TODO: unify these two fields so that there is a more unambiguous meaning of how precision is handled. // -1 is unlimited Pattern.RoundingContext.MinExponentDigits uint8 Pattern.RoundingContext.MinFractionDigits uint8 Number of digits. Pattern.RoundingContext.MinSignificantDigits uint8 Pattern.RoundingContext.Mode RoundingMode (*Formatter) Append(dst []byte, x interface{}) []byte AppendDigit appends the UTF-8 sequence for n corresponding to the given digit to dst and reports the number of bytes written. dst must be large enough to hold the rune (can be up to utf8.UTFMax bytes). Digit returns the digit for the numbering system for the corresponding ASCII value. For example, ni.Digit('3') could return '三'. Note that the argument is the rune constant '3', which equals 51, not the integer constant 3. (*Formatter) Format(dst []byte, d *Decimal) []byte InitDecimal initializes a Formatter using the default Pattern for the given language. InitEngineering initializes a Formatter using the default Pattern for the given language. InitPattern initializes a Formatter for the given Pattern. InitPerMille initializes a Formatter using the default Pattern for the given language. InitPercent initializes a Formatter using the default Pattern for the given language. InitScientific initializes a Formatter using the default Pattern for the given language. IsDecimal reports if the numbering system can convert decimal to native symbols one-to-one. (*Formatter) Render(dst []byte, d Digits) []byte RoundFractionDigits returns the number of fraction digits an implementation of Convert may round to or n < 0 if there is no maximum or a maximum is not recommended. RoundSignificantDigits returns the number of significant digits an implementation of Convert may round to or n < 0 if there is no maximum or a maximum is not recommended. (*Formatter) SetPrecision(prec int) SetScale fixes the RoundingContext to a fixed number of fraction digits. Symbol returns the string for the given symbol type. WriteDigit writes the UTF-8 sequence for n corresponding to the given ASCII digit to dst and reports the number of bytes written. dst must be large enough to hold the rune (can be up to utf8.UTFMax bytes).
Info holds number formatting configuration data. AppendDigit appends the UTF-8 sequence for n corresponding to the given digit to dst and reports the number of bytes written. dst must be large enough to hold the rune (can be up to utf8.UTFMax bytes). Digit returns the digit for the numbering system for the corresponding ASCII value. For example, ni.Digit('3') could return '三'. Note that the argument is the rune constant '3', which equals 51, not the integer constant 3. IsDecimal reports if the numbering system can convert decimal to native symbols one-to-one. Symbol returns the string for the given symbol type. WriteDigit writes the UTF-8 sequence for n corresponding to the given ASCII digit to dst and reports the number of bytes written. dst must be large enough to hold the rune (can be up to utf8.UTFMax bytes). func InfoFromLangID(compactIndex compact.ID, numberSystem string) Info func InfoFromTag(t language.Tag) Info
Pattern holds information for formatting numbers. It is designed to hold information from CLDR number patterns. This pattern is precompiled for all patterns for all languages. Even though the number of patterns is not very large, we want to keep this small. This type is only intended for internal use. // includes prefix and suffix. First byte is prefix length. Flags PatternFlag FormatWidth uint16 GroupingSize [2]uint8 // Offset into Affix for negative prefix and suffix or 0. // Offset into Affix for prefix and suffix PadRune rune RoundingContext RoundingContext // Number of decimals to shift. Used for % and ‰. RoundingContext.Increment uint32 // May differ from printed scale. // -1 is unlimited RoundingContext.MaxIntegerDigits uint8 TODO: unify these two fields so that there is a more unambiguous meaning of how precision is handled. // -1 is unlimited RoundingContext.MinExponentDigits uint8 RoundingContext.MinFractionDigits uint8 Number of digits. RoundingContext.MinSignificantDigits uint8 RoundingContext.Mode RoundingMode RoundFractionDigits returns the number of fraction digits an implementation of Convert may round to or n < 0 if there is no maximum or a maximum is not recommended. RoundSignificantDigits returns the number of significant digits an implementation of Convert may round to or n < 0 if there is no maximum or a maximum is not recommended. (*Pattern) SetPrecision(prec int) SetScale fixes the RoundingContext to a fixed number of fraction digits. func ParsePattern(s string) (f *Pattern, err error) func (*Formatter).InitPattern(t language.Tag, pat *Pattern)
A PatternFlag is a bit mask for the flag field of a Pattern. const AlwaysDecimalSeparator const AlwaysExpSign const AlwaysSign const ElideSign const PadAfterAffix const PadAfterNumber const PadAfterPrefix const PadAfterSuffix const PadBeforeSuffix const PadMask const ParenthesisForNegative
A RoundingContext indicates how a number should be converted to digits. It contains all information needed to determine the "visible digits" as required by the pluralization rules. // Number of decimals to shift. Used for % and ‰. Increment uint32 // May differ from printed scale. // -1 is unlimited MaxIntegerDigits uint8 TODO: unify these two fields so that there is a more unambiguous meaning of how precision is handled. // -1 is unlimited MinExponentDigits uint8 MinFractionDigits uint8 Number of digits. MinSignificantDigits uint8 Mode RoundingMode RoundFractionDigits returns the number of fraction digits an implementation of Convert may round to or n < 0 if there is no maximum or a maximum is not recommended. RoundSignificantDigits returns the number of significant digits an implementation of Convert may round to or n < 0 if there is no maximum or a maximum is not recommended. (*RoundingContext) SetPrecision(prec int) SetScale fixes the RoundingContext to a fixed number of fraction digits. func FormatDigits(d *Decimal, r RoundingContext) Digits func Converter.Convert(d *Decimal, r RoundingContext) func (*Decimal).Convert(r RoundingContext, number interface{}) func (*Decimal).ConvertFloat(r RoundingContext, x float64, size int) func (*Decimal).ConvertInt(r RoundingContext, signed bool, x uint64)
RoundingMode determines how a number is rounded to the desired precision. ( RoundingMode) String() string RoundingMode : expvar.Var RoundingMode : fmt.Stringer const AwayFromZero const ToNearestAway const ToNearestEven const ToNearestZero const ToNegativeInf const ToPositiveInf const ToZero
A SymbolType identifies a symbol of a specific kind. func Info.Symbol(t SymbolType) string const NumSymbolTypes const SymDecimal const SymExponential const SymGroup const SymInfinity const SymList const SymMinusSign const SymNan const SymPercentSign const SymPerMille const SymPlusSign const SymSuperscriptingExponent const SymTimeSeparator
A VisibleDigits computes digits, comma placement and trailing zeros as they will be shown to the user. ( VisibleDigits) Digits(buf []byte, t language.Tag, scale int) Digits golang.org/x/text/number.Formatter
Package-Level Functions (total 4)
InfoFromLangID returns a Info for the given compact language identifier and numbering system identifier. If system is the empty string, the default numbering system will be taken for that language.
InfoFromTag returns a Info for the given language tag.
ParsePattern extracts formatting information from a CLDR number pattern. See https://unicode.org/reports/tr35/tr35-numbers.html#Number_Format_Patterns.
Package-Level Constants (total 33)
const AwayFromZero RoundingMode = 6 // away from zero
CLDRVersion is the CLDR version from which the tables in this package are derived.
const ElideSign PatternFlag = 2 // Use space instead of plus sign. AlwaysSign must be true.
const PadBeforePrefix = 0 // Default
const PadMask PatternFlag = 96
const ParenthesisForNegative PatternFlag = 16 // Common pattern. Saves space.
const SymGroup SymbolType = 1
const SymList SymbolType = 2
const SymNan SymbolType = 10
const ToNearestAway RoundingMode = 2 // towards the nearest integer, or away from zero if equidistant.
const ToNearestEven RoundingMode = 0 // towards the nearest integer, or towards an even number if equidistant.
const ToNearestZero RoundingMode = 1 // towards the nearest integer, or towards zero if equidistant.
const ToNegativeInf RoundingMode = 4 // towards negative infinity
const ToPositiveInf RoundingMode = 3 // towards infinity
const ToZero RoundingMode = 5 // towards zero