Skip to content

Commit 9ec74ab

Browse files
committed
Don't strip zeroes from numbers that don't have decimal points
Fixes #106
1 parent 269a863 commit 9ec74ab

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

ftoa.go

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import (
66
)
77

88
func stripTrailingZeros(s string) string {
9+
if !strings.ContainsRune(s, '.') {
10+
return s
11+
}
912
offset := len(s) - 1
1013
for offset > 0 {
1114
if s[offset] == '.' {

ftoa_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
func TestFtoa(t *testing.T) {
1515
testList{
1616
{"200", Ftoa(200), "200"},
17+
{"20", Ftoa(20.0), "20"},
1718
{"2", Ftoa(2), "2"},
1819
{"2.2", Ftoa(2.2), "2.2"},
1920
{"2.02", Ftoa(2.02), "2.02"},
@@ -24,6 +25,7 @@ func TestFtoa(t *testing.T) {
2425
func TestFtoaWithDigits(t *testing.T) {
2526
testList{
2627
{"1.23, 0", FtoaWithDigits(1.23, 0), "1"},
28+
{"20, 0", FtoaWithDigits(20.0, 0), "20"},
2729
{"1.23, 1", FtoaWithDigits(1.23, 1), "1.2"},
2830
{"1.23, 2", FtoaWithDigits(1.23, 2), "1.23"},
2931
{"1.23, 3", FtoaWithDigits(1.23, 3), "1.23"},

si_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,20 @@ func BenchmarkParseSI(b *testing.B) {
126126
ParseSI("2.2346ZB")
127127
}
128128
}
129+
130+
// There was a report that zeroes were being truncated incorrectly
131+
func TestBug106(t *testing.T) {
132+
tests := []struct{
133+
in float64
134+
want string
135+
}{
136+
{20.0, "20 U"},
137+
{200.0, "200 U"},
138+
}
139+
140+
for _, test := range tests {
141+
if got :=SIWithDigits(test.in, 0, "U") ; got != test.want {
142+
t.Errorf("on %f got %v, want %v", test.in, got, test.want);
143+
}
144+
}
145+
}

0 commit comments

Comments
 (0)