Skip to content

Commit

Permalink
Merge pull request #449 from 99designs/increase-float-precision
Browse files Browse the repository at this point in the history
Increase float precision
  • Loading branch information
vektah committed Nov 28, 2018
2 parents c558979 + c45546e commit e4bad0e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion graphql/float.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func MarshalFloat(f float64) Marshaler {
return WriterFunc(func(w io.Writer) {
io.WriteString(w, fmt.Sprintf("%f", f))
io.WriteString(w, fmt.Sprintf("%g", f))
})
}

Expand Down
22 changes: 22 additions & 0 deletions graphql/float_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package graphql

import (
"bytes"
"testing"

"github.com/stretchr/testify/assert"
)

func TestFloat(t *testing.T) {
assert.Equal(t, "123", m2s(MarshalFloat(123)))
assert.Equal(t, "1.2345678901", m2s(MarshalFloat(1.2345678901)))
assert.Equal(t, "1.2345678901234567", m2s(MarshalFloat(1.234567890123456789)))
assert.Equal(t, "1.2e+20", m2s(MarshalFloat(1.2e+20)))
assert.Equal(t, "1.2e-20", m2s(MarshalFloat(1.2e-20)))
}

func m2s(m Marshaler) string {
var b bytes.Buffer
m.MarshalGQL(&b)
return b.String()
}
2 changes: 1 addition & 1 deletion graphql/jsonw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ func TestJsonWriter(t *testing.T) {
b := &bytes.Buffer{}
obj.MarshalGQL(b)

require.Equal(t, `{"test":10,"array":[1,"2",true,false,null,1.300000,true],"emptyArray":[],"child":{"child":{"child":null}}}`, b.String())
require.Equal(t, `{"test":10,"array":[1,"2",true,false,null,1.3,true],"emptyArray":[],"child":{"child":{"child":null}}}`, b.String())
}

0 comments on commit e4bad0e

Please sign in to comment.