Why Cloud save is giving too many decimals

I am trying to save my character data using cloud save. I have a serializable class which holds some data about the character like speed, attack etc. My data is saved nicely in cloud but when I opened it using the dashboard i saw UGS made too many numbers after the decimal point. Suppose my speed is a float value and its set as 0.2f but UGS is saving it as 0.2000065 something like that. Does anyone know why and what i can do to fix this? any help is appreciated, Thanks!

Here is the JSON data from UGS dashboard

Here is my save and load function for saving in cloud.

public static async Task<T> LoadData<T>(string p_key, T p_defaultData)
{
    try
    {
        var data = await CloudSaveService.Instance.Data.Player.LoadAsync(new HashSet<string> { p_key });
        if (data.TryGetValue(p_key, out var value))
        {
            string JSON = value.Value.GetAsString();
            T extractedData = JsonUtility.FromJson<T>(JSON);
            Debug.Log("loaded: "+ReflectionUtils.PrintDataToString(extractedData));
            return extractedData;
        }
        else
        {
            Debug.Log("Created: "+ReflectionUtils.PrintDataToString(p_defaultData));
            await SaveData(p_key, p_defaultData);
            return p_defaultData;
        }
    }
    catch (Exception ex)
    {
        Debug.Log(ex.Message.ToString());
        return p_defaultData;
    }
}

public static async Task SaveData<T>(string p_key, T p_data)
{
    try
    {
        string JSON = JsonUtility.ToJson(p_data);
        var convertedData = new Dictionary<string, object>
        {
            { p_key, JSON}
        };
        await CloudSaveService.Instance.Data.Player.SaveAsync(convertedData);
        Debug.Log("Saved: " + ReflectionUtils.PrintDataToString(p_data));
    }
    catch (System.Security.Authentication.AuthenticationException ex) { Debug.LogException(ex); }
    catch (RequestFailedException ex) { Debug.LogException(ex); }
}

The why is because of limited floating point precision. There’s only so much bits to encode any number, and less so for a number that has decimal digits.

On the cloud, float may be saved as double, hence you get much more decimal digits. The question is, does this get transferred as a json response to the client? I’m not aware of how that looks, but that’s what matters.

You can also completely avoid all this if you store your data as binary (byte array converted to string), possibly even compressed. Cloud Save accepts any string verbatim, it does not require the string to be Json formatted. It does limit your inspection ability in the cloud dashboard of course.

1 Like

I was saving it using gzip compression but as you said it limits my ability to see the data on the dashboard. Maybe i will try something else to see if i can reduce the decimal numbers because my saved data is also increasing because of so many extra digits

Btw .NET has built-in BrotliStream compression, it’s typically faster and more efficient.