Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically converting float to integer loses precision #99

Closed
nphmuller opened this issue Aug 12, 2020 · 0 comments · Fixed by #108
Closed

Automatically converting float to integer loses precision #99

nphmuller opened this issue Aug 12, 2020 · 0 comments · Fixed by #108

Comments

@nphmuller
Copy link

I'd expect the following test to succeed, but it doesn't. This is caused by the way float64 values are converted to integers in the current implementation. The tests demonstrates why json.Unmarshall probably chooses to unmarshall all numeric json values to float64 when unmarshalling to an interface.

func TestConversionJSONFloatRounded(t *testing.T) {
	jsonString :=
		`{
    "a": 1.00,
    "b": {
      "data": 1.00
    },
    "c": [1.00],
    "d": [[1.00]]
  }`
	m, err := objx.FromJSON(jsonString)

	assert.Nil(t, err)
	require.NotNil(t, m)
	assert.Equal(t, 1.00, m.Get("a").Float64())
	assert.Equal(t, 1.00, m.Get("b.data").Float64())

	assert.True(t, m.Get("c").IsInterSlice())
	assert.Equal(t, 1.00, m.Get("c").InterSlice()[0])

	assert.True(t, m.Get("d").IsInterSlice())
	assert.Equal(t, []interface{}{1.00}, m.Get("d").InterSlice()[0])
}

A possible fix that could still support MustInt() etc. could be to do the conversion in the MustInt() methods. That way the expected float64 behaviour would persist, but integers could still be supported for ease of use.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant