1

I don't know if this is a bug in Play! or if there is another cause, but here is what is happening.

I have an object (MyObject) that has a member variable of type Float.

public class MyObject {
    private Float data;
}

When using Json.toJson() (from play.libs.Json) to send the response containing a MyObject item, the values get scrambled.

As an example:

MyObject myObj = new MyObj();
myObj.setData(3.2f);

JSON string:

{
    "data": 3.200000047683716
}

If I change the data type to Double, everything works fine.

I suspect it may be because of float precision vs double precision and how JSON.toJson() handles Float objects, however, in debug, myObj.data is 3.2.

Kristján
  • 18,165
  • 5
  • 50
  • 62
Petre Popescu
  • 1,950
  • 3
  • 24
  • 38

1 Answers1

2

Yes, the problem is that 3.2 cannot be accurately represented by a Float. It actually can't be by a Double either, but the error is small enough that it gets thrown away.

Handy references:

Kristján
  • 18,165
  • 5
  • 50
  • 62