Skip to content

Commit

Permalink
Don't strip zeroes from numbers that don't have decimal points
Browse files Browse the repository at this point in the history
Fixes #106
  • Loading branch information
dustin committed Jan 10, 2023
1 parent 269a863 commit 9ec74ab
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ftoa.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import (
)

func stripTrailingZeros(s string) string {
if !strings.ContainsRune(s, '.') {
return s
}
offset := len(s) - 1
for offset > 0 {
if s[offset] == '.' {
Expand Down
2 changes: 2 additions & 0 deletions ftoa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
func TestFtoa(t *testing.T) {
testList{
{"200", Ftoa(200), "200"},
{"20", Ftoa(20.0), "20"},
{"2", Ftoa(2), "2"},
{"2.2", Ftoa(2.2), "2.2"},
{"2.02", Ftoa(2.02), "2.02"},
Expand All @@ -24,6 +25,7 @@ func TestFtoa(t *testing.T) {
func TestFtoaWithDigits(t *testing.T) {
testList{
{"1.23, 0", FtoaWithDigits(1.23, 0), "1"},
{"20, 0", FtoaWithDigits(20.0, 0), "20"},
{"1.23, 1", FtoaWithDigits(1.23, 1), "1.2"},
{"1.23, 2", FtoaWithDigits(1.23, 2), "1.23"},
{"1.23, 3", FtoaWithDigits(1.23, 3), "1.23"},
Expand Down
17 changes: 17 additions & 0 deletions si_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,20 @@ func BenchmarkParseSI(b *testing.B) {
ParseSI("2.2346ZB")
}
}

// There was a report that zeroes were being truncated incorrectly
func TestBug106(t *testing.T) {
tests := []struct{
in float64
want string
}{
{20.0, "20 U"},
{200.0, "200 U"},
}

for _, test := range tests {
if got :=SIWithDigits(test.in, 0, "U") ; got != test.want {
t.Errorf("on %f got %v, want %v", test.in, got, test.want);
}
}
}

0 comments on commit 9ec74ab

Please sign in to comment.