Added Commaf
This isn't the most efficient solution, but it should be correct in most
cases. If you find one that isn't, please contribute a test.
Closes #17
diff --git a/comma.go b/comma.go
index 9efc682..8bd7862 100644
--- a/comma.go
+++ b/comma.go
@@ -35,6 +35,20 @@
return sign + strings.Join(parts[j:len(parts)], ",")
}
+// Commaf produces a string form of the given number in base 10 with
+// commas after every three orders of magnitude.
+//
+// e.g. Comma(834142.32) -> 834,142.32
+func Commaf(v float64) string {
+ parts := strings.Split(strconv.FormatFloat(v, 'f', -1, 64), ".")
+ i, _ := (&big.Int{}).SetString(parts[0], 10)
+ rv := BigComma(i)
+ if len(parts) > 1 {
+ rv += "." + parts[1]
+ }
+ return rv
+}
+
// BigComma produces a string form of the given big.Int in base 10
// with commas after every three orders of magnitude.
func BigComma(b *big.Int) string {
diff --git a/comma_test.go b/comma_test.go
index 180fbee..1386bd1 100644
--- a/comma_test.go
+++ b/comma_test.go
@@ -1,6 +1,7 @@
package humanize
import (
+ "math"
"math/big"
"testing"
)
@@ -33,6 +34,34 @@
}.validate(t)
}
+func TestCommafs(t *testing.T) {
+ testList{
+ {"0", Commaf(0), "0"},
+ {"10.11", Commaf(10.11), "10.11"},
+ {"100", Commaf(100), "100"},
+ {"1,000", Commaf(1000), "1,000"},
+ {"10,000", Commaf(10000), "10,000"},
+ {"100,000", Commaf(100000), "100,000"},
+ {"10,000,000", Commaf(10000000), "10,000,000"},
+ {"10,100,000", Commaf(10100000), "10,100,000"},
+ {"10,010,000", Commaf(10010000), "10,010,000"},
+ {"10,001,000", Commaf(10001000), "10,001,000"},
+ {"123,456,789", Commaf(123456789), "123,456,789"},
+ {"maxf64", Commaf(math.MaxFloat64), "179,769,313,486,231,570,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000"},
+ {"minf64", Commaf(math.SmallestNonzeroFloat64), "0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005"},
+ {"-123,456,789", Commaf(-123456789), "-123,456,789"},
+ {"-10,100,000", Commaf(-10100000), "-10,100,000"},
+ {"-10,010,000", Commaf(-10010000), "-10,010,000"},
+ {"-10,001,000", Commaf(-10001000), "-10,001,000"},
+ {"-10,000,000", Commaf(-10000000), "-10,000,000"},
+ {"-100,000", Commaf(-100000), "-100,000"},
+ {"-10,000", Commaf(-10000), "-10,000"},
+ {"-1,000", Commaf(-1000), "-1,000"},
+ {"-100.11", Commaf(-100.11), "-100.11"},
+ {"-10", Commaf(-10), "-10"},
+ }.validate(t)
+}
+
func BenchmarkCommas(b *testing.B) {
for i := 0; i < b.N; i++ {
Comma(1234567890)