Make it compatible again with go < 1.5
diff --git a/comma.go b/comma.go
index 3cbd3ca..b65ea6f 100644
--- a/comma.go
+++ b/comma.go
@@ -99,34 +99,3 @@
parts[j] = strconv.Itoa(int(b.Int64()))
return sign + strings.Join(parts[j:], ",")
}
-
-// 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()
-}
diff --git a/comma_go15.go b/comma_go15.go
new file mode 100644
index 0000000..9d27cdc
--- /dev/null
+++ b/comma_go15.go
@@ -0,0 +1,40 @@
+// +build go1.5
+
+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()
+}
diff --git a/comma_go15_test.go b/comma_go15_test.go
new file mode 100644
index 0000000..baf777b
--- /dev/null
+++ b/comma_go15_test.go
@@ -0,0 +1,44 @@
+// +build go1.5
+
+package humanize
+
+import (
+ "math"
+ "math/big"
+ "testing"
+)
+
+func BenchmarkBigCommaf(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ Commaf(1234567890.83584)
+ }
+}
+
+func TestBigCommafs(t *testing.T) {
+ testList{
+ {"0", BigCommaf(big.NewFloat(0)), "0"},
+ {"10.11", BigCommaf(big.NewFloat(10.11)), "10.11"},
+ {"100", BigCommaf(big.NewFloat(100)), "100"},
+ {"1,000", BigCommaf(big.NewFloat(1000)), "1,000"},
+ {"10,000", BigCommaf(big.NewFloat(10000)), "10,000"},
+ {"100,000", BigCommaf(big.NewFloat(100000)), "100,000"},
+ {"834,142.32", BigCommaf(big.NewFloat(834142.32)), "834,142.32"},
+ {"10,000,000", BigCommaf(big.NewFloat(10000000)), "10,000,000"},
+ {"10,100,000", BigCommaf(big.NewFloat(10100000)), "10,100,000"},
+ {"10,010,000", BigCommaf(big.NewFloat(10010000)), "10,010,000"},
+ {"10,001,000", BigCommaf(big.NewFloat(10001000)), "10,001,000"},
+ {"123,456,789", BigCommaf(big.NewFloat(123456789)), "123,456,789"},
+ {"maxf64", BigCommaf(big.NewFloat(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", BigCommaf(big.NewFloat(math.SmallestNonzeroFloat64)), "0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004940656458412465"},
+ {"-123,456,789", BigCommaf(big.NewFloat(-123456789)), "-123,456,789"},
+ {"-10,100,000", BigCommaf(big.NewFloat(-10100000)), "-10,100,000"},
+ {"-10,010,000", BigCommaf(big.NewFloat(-10010000)), "-10,010,000"},
+ {"-10,001,000", BigCommaf(big.NewFloat(-10001000)), "-10,001,000"},
+ {"-10,000,000", BigCommaf(big.NewFloat(-10000000)), "-10,000,000"},
+ {"-100,000", BigCommaf(big.NewFloat(-100000)), "-100,000"},
+ {"-10,000", BigCommaf(big.NewFloat(-10000)), "-10,000"},
+ {"-1,000", BigCommaf(big.NewFloat(-1000)), "-1,000"},
+ {"-100.11", BigCommaf(big.NewFloat(-100.11)), "-100.11"},
+ {"-10", BigCommaf(big.NewFloat(-10)), "-10"},
+ }.validate(t)
+}
diff --git a/comma_test.go b/comma_test.go
index 734b60d..49040fb 100644
--- a/comma_test.go
+++ b/comma_test.go
@@ -81,12 +81,6 @@
}
}
-func BenchmarkBigCommaf(b *testing.B) {
- for i := 0; i < b.N; i++ {
- Commaf(1234567890.83584)
- }
-}
-
func bigComma(i int64) string {
return BigComma(big.NewInt(i))
}
@@ -138,32 +132,3 @@
}
}
}
-
-func TestBigCommafs(t *testing.T) {
- testList{
- {"0", BigCommaf(big.NewFloat(0)), "0"},
- {"10.11", BigCommaf(big.NewFloat(10.11)), "10.11"},
- {"100", BigCommaf(big.NewFloat(100)), "100"},
- {"1,000", BigCommaf(big.NewFloat(1000)), "1,000"},
- {"10,000", BigCommaf(big.NewFloat(10000)), "10,000"},
- {"100,000", BigCommaf(big.NewFloat(100000)), "100,000"},
- {"834,142.32", BigCommaf(big.NewFloat(834142.32)), "834,142.32"},
- {"10,000,000", BigCommaf(big.NewFloat(10000000)), "10,000,000"},
- {"10,100,000", BigCommaf(big.NewFloat(10100000)), "10,100,000"},
- {"10,010,000", BigCommaf(big.NewFloat(10010000)), "10,010,000"},
- {"10,001,000", BigCommaf(big.NewFloat(10001000)), "10,001,000"},
- {"123,456,789", BigCommaf(big.NewFloat(123456789)), "123,456,789"},
- {"maxf64", BigCommaf(big.NewFloat(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", BigCommaf(big.NewFloat(math.SmallestNonzeroFloat64)), "0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004940656458412465"},
- {"-123,456,789", BigCommaf(big.NewFloat(-123456789)), "-123,456,789"},
- {"-10,100,000", BigCommaf(big.NewFloat(-10100000)), "-10,100,000"},
- {"-10,010,000", BigCommaf(big.NewFloat(-10010000)), "-10,010,000"},
- {"-10,001,000", BigCommaf(big.NewFloat(-10001000)), "-10,001,000"},
- {"-10,000,000", BigCommaf(big.NewFloat(-10000000)), "-10,000,000"},
- {"-100,000", BigCommaf(big.NewFloat(-100000)), "-100,000"},
- {"-10,000", BigCommaf(big.NewFloat(-10000)), "-10,000"},
- {"-1,000", BigCommaf(big.NewFloat(-1000)), "-1,000"},
- {"-100.11", BigCommaf(big.NewFloat(-100.11)), "-100.11"},
- {"-10", BigCommaf(big.NewFloat(-10)), "-10"},
- }.validate(t)
-}