number

package
v0.2.0-unicode10.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2017 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Overview

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.", number.Decimal(1234))
// Prints: 1,234 bottles of beer on the wall.

p.Printf("%v of gophers lose too much fur", 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.", number.Decimal(1.2))
// Prints: Er zijn 1,2 fietsen per huishouden.

The width and scale specified in the formatting directives override the configuration of the formatter.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FormatFunc

type FormatFunc func(x interface{}, opts ...Option) Formatter

A FormatFunc formates a number.

func NewFormat

func NewFormat(format FormatFunc, opts ...Option) FormatFunc

NewFormat creates a FormatFunc based on another FormatFunc and new options. Use NewFormat to cash the creation of formatters.

type Formatter

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

func Decimal

func Decimal(x interface{}, opts ...Option) Formatter

Decimal formats a number as a floating point decimal.

func Engineering

func Engineering(x interface{}, opts ...Option) Formatter

Engineering formats a number using engineering notation, which is like scientific notation, but with the exponent normalized to multiples of 3.

func PerMille

func PerMille(x interface{}, opts ...Option) Formatter

PerMille formats a number as a per mille indication. A value of 1.0 means 1000‰.

func Percent

func Percent(x interface{}, opts ...Option) Formatter

Percent formats a number as a percentage. A value of 1.0 means 100%.

func Scientific

func Scientific(x interface{}, opts ...Option) Formatter

Scientific formats a number in scientific format.

func (Formatter) Digits

func (f Formatter) Digits(buf []byte, tag language.Tag, scale int) number.Digits

Digits returns information about which logical digits will be presented to the user. This information is relevant, for instance, to determine plural forms.

func (Formatter) Format

func (f Formatter) Format(state format.State, verb rune)

Format implements format.Formatter. It is for internal use only for now.

type Option

type Option option

An Option configures a Formatter.

func FormatWidth

func FormatWidth(n int) Option

FormatWidth sets the total format width.

func IncrementString

func IncrementString(decimal string) Option

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.

Example
package main

import (
	"golang.org/x/text/language"
	"golang.org/x/text/message"
	"golang.org/x/text/number"
)

func main() {
	p := message.NewPrinter(language.English)

	p.Println(number.Decimal(1.33, number.IncrementString("0.50")))

}
Output:

1.50

func MaxFractionDigits

func MaxFractionDigits(max int) Option

MaxFractionDigits specifies the maximum number of fractional digits.

func MaxIntegerDigits

func MaxIntegerDigits(max int) Option

MaxIntegerDigits limits the number of integer digits, eliminating the most significant digits.

Example
package main

import (
	"golang.org/x/text/language"
	"golang.org/x/text/message"
	"golang.org/x/text/number"
)

func main() {
	const year = 1999
	p := message.NewPrinter(language.English)
	p.Println("Year:", number.Decimal(year, number.MaxIntegerDigits(2)))

}
Output:

Year: 99

func MinFractionDigits

func MinFractionDigits(min int) Option

MinFractionDigits specifies the minimum number of fractional digits.

func MinIntegerDigits

func MinIntegerDigits(min int) Option

MinIntegerDigits specifies the minimum number of integer digits, adding leading zeros when needed.

func NoSeparator

func NoSeparator() Option

NoSeparator causes a number to be displayed without grouping separators.

func Pad

func Pad(r rune) Option

Pad sets the rune to be used for filling up to the format width.

func PatternOverrides

func PatternOverrides(patterns map[string]string) Option

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.

func Precision

func Precision(prec int) Option

Precision sets the maximum number of significant digits. A negative value means exact.

func Scale

func Scale(decimals int) Option

Scale simultaneously sets MinFractionDigits and MaxFractionDigits to the given value.

Jump to

Keyboard shortcuts

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