1 // Copyright 2015 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.
12 "golang.org/x/text/internal"
13 "golang.org/x/text/internal/format"
14 "golang.org/x/text/language"
17 // Amount is an amount-currency unit pair.
19 amount interface{} // Change to decimal(64|128).
23 // Currency reports the currency unit of this amount.
24 func (a Amount) Currency() Unit { return a.currency }
26 // TODO: based on decimal type, but may make sense to customize a bit.
27 // func (a Amount) Decimal()
28 // func (a Amount) Int() (int64, error)
29 // func (a Amount) Fraction() (int64, error)
30 // func (a Amount) Rat() *big.Rat
31 // func (a Amount) Float() (float64, error)
32 // func (a Amount) Scale() uint
33 // func (a Amount) Precision() uint
34 // func (a Amount) Sign() int
36 // Add/Sub/Div/Mul/Round.
38 var space = []byte(" ")
40 // Format implements fmt.Formatter. It accepts format.State for
41 // language-specific rendering.
42 func (a Amount) Format(s fmt.State, verb rune) {
46 format: defaultFormat,
51 // formattedValue is currency amount or unit that implements language-sensitive
53 type formattedValue struct {
55 amount interface{} // Amount, Unit, or number.
59 // Format implements fmt.Formatter. It accepts format.State for
60 // language-specific rendering.
61 func (v formattedValue) Format(s fmt.State, verb rune) {
63 if state, ok := s.(format.State); ok {
64 lang, _ = language.CompactIndex(state.Language())
67 // Get the options. Use DefaultFormat if not present.
78 io.WriteString(s, opt.symbol(lang, cur))
82 // TODO: apply currency-specific rounding
83 scale, _ := opt.kind.Rounding(cur)
84 if _, ok := s.Precision(); !ok {
85 fmt.Fprintf(s, "%.*f", scale, v.amount)
87 fmt.Fprint(s, v.amount)
92 // Formatter decorates a given number, Unit or Amount with formatting options.
93 type Formatter func(amount interface{}) formattedValue
95 // func (f Formatter) Options(opts ...Option) Formatter
97 // TODO: call this a Formatter or FormatFunc?
99 var dummy = USD.Amount(0)
101 // adjust creates a new Formatter based on the adjustments of fn on f.
102 func (f Formatter) adjust(fn func(*options)) Formatter {
103 var o options = *(f(dummy).format)
108 // Default creates a new Formatter that defaults to currency unit c if a numeric
109 // value is passed that is not associated with a currency.
110 func (f Formatter) Default(currency Unit) Formatter {
111 return f.adjust(func(o *options) { o.currency = currency })
114 // Kind sets the kind of the underlying currency unit.
115 func (f Formatter) Kind(k Kind) Formatter {
116 return f.adjust(func(o *options) { o.kind = k })
119 var defaultFormat *options = ISO(dummy).format
122 // Uses Narrow symbols. Overrides Symbol, if present.
123 NarrowSymbol Formatter = Formatter(formNarrow)
125 // Use Symbols instead of ISO codes, when available.
126 Symbol Formatter = Formatter(formSymbol)
128 // Use ISO code as symbol.
129 ISO Formatter = Formatter(formISO)
132 // // Use full name as symbol.
136 // options configures rendering and rounding options for an Amount.
137 type options struct {
141 symbol func(compactIndex int, c Unit) string
144 func (o *options) format(amount interface{}) formattedValue {
145 v := formattedValue{format: o}
146 switch x := amount.(type) {
149 v.currency = x.currency
152 v.currency = x.currency
158 if o.currency.index == 0 {
159 panic("cannot format number without a currency being set")
161 // TODO: Must be a number.
163 v.currency = o.currency
169 optISO = options{symbol: lookupISO}
170 optSymbol = options{symbol: lookupSymbol}
171 optNarrow = options{symbol: lookupNarrow}
174 // These need to be functions, rather than curried methods, as curried methods
175 // are evaluated at init time, causing tables to be included unconditionally.
176 func formISO(x interface{}) formattedValue { return optISO.format(x) }
177 func formSymbol(x interface{}) formattedValue { return optSymbol.format(x) }
178 func formNarrow(x interface{}) formattedValue { return optNarrow.format(x) }
180 func lookupISO(x int, c Unit) string { return c.String() }
181 func lookupSymbol(x int, c Unit) string { return normalSymbol.lookup(x, c) }
182 func lookupNarrow(x int, c Unit) string { return narrowSymbol.lookup(x, c) }
184 type symbolIndex struct {
185 index []uint16 // position corresponds with compact index of language.
190 normalSymbol = symbolIndex{normalLangIndex, normalSymIndex}
191 narrowSymbol = symbolIndex{narrowLangIndex, narrowSymIndex}
194 func (x *symbolIndex) lookup(lang int, c Unit) string {
196 index := x.data[x.index[lang]:x.index[lang+1]]
197 i := sort.Search(len(index), func(i int) bool {
198 return index[i].cur >= c.index
200 if i < len(index) && index[i].cur == c.index {
203 end := start + uint16(symbols[x])
207 return symbols[start:end]
212 lang = int(internal.Parent[lang])