...

Package number

import "golang.org/x/text/internal/number"
Overview
Index

Overview ▾

Package number contains tools and data for formatting numbers.

Index ▾

Constants
type Converter
type Decimal
    func (d *Decimal) Convert(r RoundingContext, number interface{})
    func (d *Decimal) ConvertFloat(r RoundingContext, x float64, size int)
    func (d *Decimal) ConvertInt(r RoundingContext, signed bool, x uint64)
    func (x *Decimal) String() string
type Digits
    func FormatDigits(d *Decimal, r RoundingContext) Digits
    func (d *Digits) NumFracDigits() int
type Formatter
    func (f *Formatter) Append(dst []byte, x interface{}) []byte
    func (f *Formatter) Format(dst []byte, d *Decimal) []byte
    func (f *Formatter) InitDecimal(t language.Tag)
    func (f *Formatter) InitEngineering(t language.Tag)
    func (f *Formatter) InitPattern(t language.Tag, pat *Pattern)
    func (f *Formatter) InitPerMille(t language.Tag)
    func (f *Formatter) InitPercent(t language.Tag)
    func (f *Formatter) InitScientific(t language.Tag)
    func (f *Formatter) Render(dst []byte, d Digits) []byte
type Info
    func InfoFromLangID(compactIndex compact.ID, numberSystem string) Info
    func InfoFromTag(t language.Tag) Info
    func (n Info) AppendDigit(dst []byte, digit byte) []byte
    func (n Info) Digit(asciiDigit rune) rune
    func (n Info) IsDecimal() bool
    func (n Info) Symbol(t SymbolType) string
    func (n Info) WriteDigit(dst []byte, asciiDigit rune) int
type Pattern
    func ParsePattern(s string) (f *Pattern, err error)
type PatternFlag
type RoundingContext
    func (r *RoundingContext) RoundFractionDigits() (n int)
    func (r *RoundingContext) RoundSignificantDigits() (n int)
    func (r *RoundingContext) SetPrecision(prec int)
    func (r *RoundingContext) SetScale(scale int)
type RoundingMode
    func (i RoundingMode) String() string
type SymbolType
type VisibleDigits

Package files

common.go decimal.go format.go number.go pattern.go roundingmode_string.go tables.go

Constants

const (
    AlwaysSign PatternFlag = 1 << iota
    ElideSign              // Use space instead of plus sign. AlwaysSign must be true.
    AlwaysExpSign
    AlwaysDecimalSeparator
    ParenthesisForNegative // Common pattern. Saves space.

    PadAfterNumber
    PadAfterAffix

    PadBeforePrefix = 0 // Default
    PadAfterPrefix  = PadAfterAffix
    PadBeforeSuffix = PadAfterNumber
    PadAfterSuffix  = PadAfterNumber | PadAfterAffix
    PadMask         = PadAfterNumber | PadAfterAffix
)

CLDRVersion is the CLDR version from which the tables in this package are derived.

const CLDRVersion = "32"

type Converter

A Converter converts a number into decimals according to the given rounding criteria.

type Converter interface {
    Convert(d *Decimal, r RoundingContext)
}

type Decimal

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
type Decimal struct {
    // contains filtered or unexported fields
}

func (*Decimal) Convert

func (d *Decimal) Convert(r RoundingContext, number interface{})

Convert converts the given number to the decimal representation using the supplied RoundingContext.

func (*Decimal) ConvertFloat

func (d *Decimal) ConvertFloat(r RoundingContext, x float64, size int)

ConvertFloat converts a floating point number to decimals.

func (*Decimal) ConvertInt

func (d *Decimal) ConvertInt(r RoundingContext, signed bool, x uint64)

ConvertInt converts an integer to decimals.

func (*Decimal) String

func (x *Decimal) String() string

type Digits

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
type Digits struct {

    // End indicates the end position of the number.
    End int32 // For decimals Exp <= End. For scientific len(Digits) <= End.
    // Comma is used for the comma position for scientific (always 0 or 1) and
    // engineering notation (always 0, 1, 2, or 3).
    Comma uint8
    // IsScientific indicates whether this number is to be rendered as a
    // scientific number.
    IsScientific bool
    // contains filtered or unexported fields
}

func FormatDigits

func FormatDigits(d *Decimal, r RoundingContext) Digits

func (*Digits) NumFracDigits

func (d *Digits) NumFracDigits() int

type Formatter

Formatter contains all the information needed to render a number.

type Formatter struct {
    Pattern
    Info
}

func (*Formatter) Append

func (f *Formatter) Append(dst []byte, x interface{}) []byte

func (*Formatter) Format

func (f *Formatter) Format(dst []byte, d *Decimal) []byte

func (*Formatter) InitDecimal

func (f *Formatter) InitDecimal(t language.Tag)

InitDecimal initializes a Formatter using the default Pattern for the given language.

func (*Formatter) InitEngineering

func (f *Formatter) InitEngineering(t language.Tag)

InitEngineering initializes a Formatter using the default Pattern for the given language.

func (*Formatter) InitPattern

func (f *Formatter) InitPattern(t language.Tag, pat *Pattern)

InitPattern initializes a Formatter for the given Pattern.

func (*Formatter) InitPerMille

func (f *Formatter) InitPerMille(t language.Tag)

InitPerMille initializes a Formatter using the default Pattern for the given language.

func (*Formatter) InitPercent

func (f *Formatter) InitPercent(t language.Tag)

InitPercent initializes a Formatter using the default Pattern for the given language.

func (*Formatter) InitScientific

func (f *Formatter) InitScientific(t language.Tag)

InitScientific initializes a Formatter using the default Pattern for the given language.

func (*Formatter) Render

func (f *Formatter) Render(dst []byte, d Digits) []byte

type Info

Info holds number formatting configuration data.

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

func InfoFromLangID

func InfoFromLangID(compactIndex compact.ID, numberSystem string) Info

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.

func InfoFromTag

func InfoFromTag(t language.Tag) Info

InfoFromTag returns a Info for the given language tag.

func (Info) AppendDigit

func (n Info) AppendDigit(dst []byte, digit byte) []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).

func (Info) Digit

func (n Info) Digit(asciiDigit rune) rune

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.

func (Info) IsDecimal

func (n Info) IsDecimal() bool

IsDecimal reports if the numbering system can convert decimal to native symbols one-to-one.

func (Info) Symbol

func (n Info) Symbol(t SymbolType) string

Symbol returns the string for the given symbol type.

func (Info) WriteDigit

func (n Info) WriteDigit(dst []byte, asciiDigit rune) int

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

type Pattern

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.

type Pattern struct {
    RoundingContext

    Affix       string // includes prefix and suffix. First byte is prefix length.
    Offset      uint16 // Offset into Affix for prefix and suffix
    NegOffset   uint16 // Offset into Affix for negative prefix and suffix or 0.
    PadRune     rune
    FormatWidth uint16

    GroupingSize [2]uint8
    Flags        PatternFlag
}

func ParsePattern

func ParsePattern(s string) (f *Pattern, err error)

ParsePattern extracts formatting information from a CLDR number pattern.

See https://unicode.org/reports/tr35/tr35-numbers.html#Number_Format_Patterns.

type PatternFlag

A PatternFlag is a bit mask for the flag field of a Pattern.

type PatternFlag uint8

type RoundingContext

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.

type RoundingContext struct {
    // TODO: unify these two fields so that there is a more unambiguous meaning
    // of how precision is handled.
    MaxSignificantDigits int16 // -1 is unlimited
    MaxFractionDigits    int16 // -1 is unlimited

    Increment      uint32
    IncrementScale uint8 // May differ from printed scale.

    Mode RoundingMode

    DigitShift uint8 // Number of decimals to shift. Used for % and ‰.

    // Number of digits.
    MinIntegerDigits uint8

    MaxIntegerDigits     uint8
    MinFractionDigits    uint8
    MinSignificantDigits uint8

    MinExponentDigits uint8
}

func (*RoundingContext) RoundFractionDigits

func (r *RoundingContext) RoundFractionDigits() (n int)

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.

func (*RoundingContext) RoundSignificantDigits

func (r *RoundingContext) RoundSignificantDigits() (n int)

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.

func (*RoundingContext) SetPrecision

func (r *RoundingContext) SetPrecision(prec int)

func (*RoundingContext) SetScale

func (r *RoundingContext) SetScale(scale int)

SetScale fixes the RoundingContext to a fixed number of fraction digits.

type RoundingMode

RoundingMode determines how a number is rounded to the desired precision.

type RoundingMode byte
const (
    ToNearestEven RoundingMode = iota // towards the nearest integer, or towards an even number if equidistant.
    ToNearestZero                     // towards the nearest integer, or towards zero if equidistant.
    ToNearestAway                     // towards the nearest integer, or away from zero if equidistant.
    ToPositiveInf                     // towards infinity
    ToNegativeInf                     // towards negative infinity
    ToZero                            // towards zero
    AwayFromZero                      // away from zero

)

func (RoundingMode) String

func (i RoundingMode) String() string

type SymbolType

A SymbolType identifies a symbol of a specific kind.

type SymbolType int
const (
    SymDecimal SymbolType = iota
    SymGroup
    SymList
    SymPercentSign
    SymPlusSign
    SymMinusSign
    SymExponential
    SymSuperscriptingExponent
    SymPerMille
    SymInfinity
    SymNan
    SymTimeSeparator

    NumSymbolTypes
)

type VisibleDigits

A VisibleDigits computes digits, comma placement and trailing zeros as they will be shown to the user.

type VisibleDigits interface {
    Digits(buf []byte, t language.Tag, scale int) Digits
}