Skip to content

Commit

Permalink
[req] Better floats encoding in 'Query'
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Nov 26, 2024
1 parent 64af50c commit 4d39d95
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
3 changes: 2 additions & 1 deletion req/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"bytes"
"fmt"
"net/url"
"strings"

"github.com/essentialkaos/ek/v13/mathutil"
)
Expand Down Expand Up @@ -187,7 +188,7 @@ func queryFormatNumber(v any) string {
}

func queryFormatFloat(v any) string {
return fmt.Sprintf("%f", v)
return strings.TrimRight(strings.TrimRight(fmt.Sprintf("%f", v), "0"), ".")
}

func queryFormatStringSlice(buf *bytes.Buffer, v []string) {
Expand Down
12 changes: 6 additions & 6 deletions req/req_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -716,8 +716,8 @@ func (s *ReqSuite) TestQueryParsing(c *C) {
"test29": []uint16{0, 1, 2},
"test30": []uint32{0, 1, 2},
"test31": []uint64{0, 1, 2},
"test32": []float32{0, 1, 2},
"test33": []float64{0, 1, 2},
"test32": []float32{0.01, 1.0, 2.231213},
"test33": []float64{0.01, 1.0, 2.231213},
}

nq, err := url.ParseQuery(q.Encode())
Expand All @@ -740,8 +740,8 @@ func (s *ReqSuite) TestQueryParsing(c *C) {
c.Assert(nq.Get("test12"), Equals, "8")
c.Assert(nq.Get("test13"), Equals, "9")
c.Assert(nq.Get("test14"), Equals, "10")
c.Assert(nq.Get("test15"), Equals, "12.350000")
c.Assert(nq.Get("test16"), Equals, "56.789500")
c.Assert(nq.Get("test15"), Equals, "12.35")
c.Assert(nq.Get("test16"), Equals, "56.7895")
c.Assert(nq.Has("test17"), Equals, true)
c.Assert(nq.Get("test17"), Equals, "")
c.Assert(nq.Get("test18"), Equals, "test")
Expand All @@ -758,8 +758,8 @@ func (s *ReqSuite) TestQueryParsing(c *C) {
c.Assert(nq.Get("test29"), Equals, "0,1,2")
c.Assert(nq.Get("test30"), Equals, "0,1,2")
c.Assert(nq.Get("test31"), Equals, "0,1,2")
c.Assert(nq.Get("test32"), Equals, "0.000000,1.000000,2.000000")
c.Assert(nq.Get("test33"), Equals, "0.000000,1.000000,2.000000")
c.Assert(nq.Get("test32"), Equals, "0.01,1,2.231213")
c.Assert(nq.Get("test33"), Equals, "0.01,1,2.231213")
}

// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down

0 comments on commit 4d39d95

Please sign in to comment.