package number
import (
"fmt"
"strings"
"golang.org/x/text/feature/plural"
"golang.org/x/text/internal/format"
"golang.org/x/text/internal/number"
"golang.org/x/text/language"
)
type FormatFunc func (x interface {}, opts ...Option ) Formatter
func NewFormat (format FormatFunc , opts ...Option ) FormatFunc {
o := *format (nil ).options
n := len (o .options )
o .options = append (o .options [:n :n ], opts ...)
return func (x interface {}, opts ...Option ) Formatter {
return newFormatter (&o , opts , x )
}
}
type options struct {
verbs string
initFunc initFunc
options []Option
pluralFunc func (t language .Tag , scale int ) (f plural .Form , n int )
}
type optionFlag uint16
const (
hasScale optionFlag = 1 << iota
hasPrecision
noSeparator
exact
)
type initFunc func (f *number .Formatter , t language .Tag )
func newFormatter(o *options , opts []Option , value interface {}) Formatter {
if len (opts ) > 0 {
n := *o
n .options = opts
o = &n
}
return Formatter {o , value }
}
func newOptions(verbs string , f initFunc ) *options {
return &options {verbs : verbs , initFunc : f }
}
type Formatter struct {
*options
value interface {}
}
func (f Formatter ) Format (state format .State , verb rune ) {
lang := state .Language ()
if !strings .Contains (f .verbs , string (verb )) {
fmt .Fprintf (state , "%%!%s(%T=%v)" , string (verb ), f .value , f .value )
return
}
var p number .Formatter
f .initFunc (&p , lang )
for _ , o := range f .options .options {
o (lang , &p )
}
if w , ok := state .Width (); ok {
p .FormatWidth = uint16 (w )
}
if prec , ok := state .Precision (); ok {
switch verb {
case 'd' :
p .SetScale (0 )
case 'f' :
p .SetScale (prec )
case 'e' :
p .SetPrecision (prec + 1 )
case 'g' :
p .SetPrecision (prec )
}
}
var d number .Decimal
d .Convert (p .RoundingContext , f .value )
state .Write (p .Format (nil , &d ))
}
func (f Formatter ) Digits (buf []byte , tag language .Tag , scale int ) number .Digits {
var p number .Formatter
f .initFunc (&p , tag )
if scale >= 0 {
p .SetScale (scale )
}
var d number .Decimal
d .Convert (p .RoundingContext , f .value )
return number .FormatDigits (&d , p .RoundingContext )
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .