number

package
v0.0.0-...-15e26ee Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2021 License: BSD-3-Clause Imports: 8 Imported by: 0

Documentation

Overview

Package number contains tools and data for formatting numbers.

Index

Constants

View Source
const (
	AlwaysSign PatternFlag = 1 << iota
	AlwaysExpSign
	AlwaysDecimalSeparator
	ParenthesisForNegative // Common pattern. Saves space.

	PadAfterNumber
	PadAfterAffix

	PadBeforePrefix = 0 // Default
	PadAfterPrefix  = PadAfterAffix
	PadBeforeSuffix = PadAfterNumber
	PadAfterSuffix  = PadAfterNumber | PadAfterAffix
	PadMask         = PadAfterNumber | PadAfterAffix
)
View Source
const CLDRVersion = "30"

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

Variables

This section is empty.

Functions

This section is empty.

Types

type Converter

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

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

type Decimal

type Decimal struct {
	Digits []byte // mantissa digits, big-endian
	Exp    int32  // exponent
	Neg    bool
	Inf    bool // Takes precedence over Digits and Exp.
	NaN    bool // Takes precedence over Inf.
	// contains filtered or unexported fields
}

A Decimal represents floating point number represented in digits of the base in which a number is to be displayed. 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
0.00123    Digits: [1, 2, 3],       Exp: -2

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

func (x *Decimal) String() string

type Formatter

type Formatter struct {
	*Pattern
	Info
	RoundingContext
	// contains filtered or unexported fields
}

Formatter contains all the information needed to render a number.

func (*Formatter) Format

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

type Info

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

Info holds number formatting configuration data.

func InfoFromLangID

func InfoFromLangID(compactIndex int, 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

type Pattern struct {
	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.

	Multiplier     uint32
	RoundIncrement uint32 // Use Min*Digits to determine scale
	PadRune        rune

	FormatWidth uint16

	GroupingSize [2]uint8
	Flags        PatternFlag

	// Number of digits.
	MinIntegerDigits     uint8
	MaxIntegerDigits     uint8
	MinFractionDigits    uint8
	MaxFractionDigits    uint8
	MinSignificantDigits uint8
	MaxSignificantDigits uint8
	MinExponentDigits    uint8
}

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.

func ParsePattern

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

ParsePattern extracts formatting information from a CLDR number pattern.

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

type PatternFlag

type PatternFlag uint8

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

type RoundingContext

type RoundingContext struct {
	Mode      RoundingMode
	Increment int32 // if > 0, round to Increment * 10^-Scale

	Precision int32 // maximum number of significant digits.
	Scale     int32 // maximum number of decimals after the dot.
}

A RoundingContext indicates how a number should be converted to digits.

type RoundingMode

type RoundingMode byte

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

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

type SymbolType int

A SymbolType identifies a symbol of a specific kind.

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

	NumSymbolTypes
)

Jump to

Keyboard shortcuts

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