2
0
mirror of https://github.com/miguelmota/cointop synced 2024-11-16 21:25:38 +00:00
cointop/pkg/humanize/humanize.go
Miguel Mota ad9ab777d8 rm dep
Former-commit-id: 1c64d4a067c68f64d02737472b2f42d8b4868d30 [formerly 1c64d4a067c68f64d02737472b2f42d8b4868d30 [formerly 0553194e516b6433270cb4fd8c41392fc4ee22e3 [formerly 0c842dac32]]]
Former-commit-id: 512fe94317ae26c0778663381c07381b0f7cad75
Former-commit-id: 4362b8e1360c2aed98d6e9726244e2e246d7dd73 [formerly ccad131add3fe8b9b8841b2758e958a82d97a595]
Former-commit-id: 40a20d88395729060edcba639d22ba3e52c2faa5
2018-04-03 00:50:55 -07:00

41 lines
772 B
Go

package humanize
import (
"bytes"
"strconv"
"strings"
)
// 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()
}