- Home /
Why is JsonUtility.ToJson turning all my floats into doubles?
In the long run my save games end up storing thousands of floats as some players run around for 100+km. In trying to further optimize the save files I'm finding that all of the floats have 15-16 digits. Which is way more precision than I need, and when multiplied thousands of times ends up being a lot more data than I need. Is this expected behavior?
Answer by Bunny83 · Mar 31, 2018 at 02:25 PM
Json uses the double precision floating-point format for numbers. There's no way to change that. As alternative you can store your value as string and format it any way you like. Though you have to do the conversion to \ from string yourself.
are you sure Unity's JsonUtility is implemented the way the JavaScript parser from the link is?
Answer by vignesh211 · Jan 08 at 05:26 PM
Alternate way(hoping this helps someone):
using Newtonsoft.Json; // use this namespace
JsonConvert.SerializeObject(object); //use this line of code to change a object to json string
this works the same way as JsonUtility.ToJson(object) except it doesn't change the float precision.
Your answer
Follow this Question
Related Questions
Serialization errors.. 0 Answers
Hashtable as object literal serialization toJSON 0 Answers
Parsing "complex" json 1 Answer
Odd change in behaviour after I rearranged project files 0 Answers
JsonUtility spitting out weird values for unsigned long integers 2 Answers