Debian Codegraph @

number/number.go

raw
1 // Copyright 2017 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package number
6
7 // TODO:
8 // p.Printf("The gauge was at %v.", number.Spell(number.Percent(23)))
9 // // Prints: The gauge was at twenty-three percent.
10 //
11 // p.Printf("From here to %v!", number.Spell(math.Inf()))
12 // // Prints: From here to infinity!
13 //
14
15 import (
16 "golang.org/x/text/internal/number"
17 )
18
19 const (
20 decimalVerbs = "vfgd"
21 scientificVerbs = "veg"
22 )
23
24 // Decimal formats a number as a floating point decimal.
25 func Decimal(x interface{}, opts ...Option) Formatter {
26 return newFormatter(decimalOptions, opts, x)
27 }
28
29 var decimalOptions = newOptions(decimalVerbs, (*number.Formatter).InitDecimal)
30
31 // Scientific formats a number in scientific format.
32 func Scientific(x interface{}, opts ...Option) Formatter {
33 return newFormatter(scientificOptions, opts, x)
34 }
35
36 var scientificOptions = newOptions(scientificVerbs, (*number.Formatter).InitScientific)
37
38 // Engineering formats a number using engineering notation, which is like
39 // scientific notation, but with the exponent normalized to multiples of 3.
40 func Engineering(x interface{}, opts ...Option) Formatter {
41 return newFormatter(engineeringOptions, opts, x)
42 }
43
44 var engineeringOptions = newOptions(scientificVerbs, (*number.Formatter).InitEngineering)
45
46 // Percent formats a number as a percentage. A value of 1.0 means 100%.
47 func Percent(x interface{}, opts ...Option) Formatter {
48 return newFormatter(percentOptions, opts, x)
49 }
50
51 var percentOptions = newOptions(decimalVerbs, (*number.Formatter).InitPercent)
52
53 // PerMille formats a number as a per mille indication. A value of 1.0 means
54 // 1000‰.
55 func PerMille(x interface{}, opts ...Option) Formatter {
56 return newFormatter(perMilleOptions, opts, x)
57 }
58
59 var perMilleOptions = newOptions(decimalVerbs, (*number.Formatter).InitPerMille)
60
61 // TODO:
62 // - Shortest: akin to verb 'g' of 'G'
63 //
64 // TODO: RBNF forms:
65 // - Compact: 1M 3.5T
66 // - CompactBinary: 1Mi 3.5Ti
67 // - Long: 1 million
68 // - Ordinal:
69 // - Roman: MCMIIXX
70 // - RomanSmall: mcmiixx
71 // - Text: numbers as it typically appears in running text, allowing
72 // language-specific choices for when to use numbers and when to use words.
73 // - Spell?: spelled-out number. Maybe just allow as an option?
74
75 // NOTE: both spelled-out numbers and ordinals, to render correctly, need
76 // detailed linguistic information from the translated string into which they
77 // are substituted. We will need to implement that first.