You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cointop/pkg/humanize/humanize.go

56 lines
1.1 KiB
Go

package humanize
import (
"bytes"
"strconv"
"strings"
"golang.org/x/text/language"
"golang.org/x/text/message"
)
// Commaf produces a string form of the given number in base 10 with
// commas after every three orders of magnitude.
//
// e.g. Commaf(834142.32) -> 834,142.32
func Commaf(v float64) string {
buf := &bytes.Buffer{}
if v < 0 {
buf.Write([]byte{'-'})
v = 0 - v
}
comma := []byte{','}
parts := strings.Split(strconv.FormatFloat(v, 'f', -1, 64), ".")
pos := 0
if len(parts[0])%3 != 0 {
pos += len(parts[0]) % 3
buf.WriteString(parts[0][:pos])
buf.Write(comma)
}
for ; pos < len(parts[0]); pos += 3 {
buf.WriteString(parts[0][pos : pos+3])
buf.Write(comma)
}
buf.Truncate(buf.Len() - 1)
if len(parts) > 1 {
buf.Write([]byte{'.'})
buf.WriteString(parts[1])
}
return buf.String()
}
// Commaf2 ...
func Commaf2(v float64) string {
p := message.NewPrinter(language.English)
return p.Sprintf("%.2f", v)
}
// Commaf0 ...
func Commaf0(v float64) string {
p := message.NewPrinter(language.English)
return p.Sprintf("%.0f", v)
}