1
0
Fork 0
mirror of https://github.com/vbatts/imgsrv.git synced 2024-12-29 00:36:32 +00:00
imgsrv/vendor/github.com/dustin/go-humanize/commaf.go
Vincent Batts 3c732c3b43
vendoring sources, for posterity sake
Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
2019-03-11 15:31:42 -04:00

40 lines
745 B
Go

// +build go1.6
package humanize
import (
"bytes"
"math/big"
"strings"
)
// BigCommaf produces a string form of the given big.Float in base 10
// with commas after every three orders of magnitude.
func BigCommaf(v *big.Float) string {
buf := &bytes.Buffer{}
if v.Sign() < 0 {
buf.Write([]byte{'-'})
v.Abs(v)
}
comma := []byte{','}
parts := strings.Split(v.Text('f', -1), ".")
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()
}