Search Unity

Saving a Double with PlayerPrefs?

Discussion in 'Scripting' started by SpyridonZ, May 19, 2011.

  1. SpyridonZ

    SpyridonZ

    Joined:
    Jun 7, 2009
    Posts:
    97
    How would one save a variable that is a Double in to PlayerPrefs? Considering PlayerPrefs only supports string, int, and float?
     
  2. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    parseFloat(myDouble);?
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Save it as a string (convert it using ToString first), and load it as a string and parse it with double.Parse or System.Convert.ToDouble.

    --Eric
     
  4. initTechsuport

    initTechsuport

    Joined:
    May 16, 2013
    Posts:
    3
    im using ToString() to convert my double to string, then SetString to save to the PlayerPrefs, but when using either double.Parse or System.Convert.ToDouble i get a FormatExeption : Invalid Format, when i play in the editor. Im using ToString() but supposedly the exception is due to formatting issues, not sure what could be causing the problem, unless i need to use ToString() differently.
     
  5. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    I highly doubt double.ToString() is meant to be used as a proper number serializer. Google around for serializing/deserializing doubles.

    EDIT: After further research it turns out that the round trip format specifier will do the trick. In another words, if you pass in "R" as an argument for ToString() then it is guaranteed to be parsed back to the same number.

    http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx#RFormatString
     
    Last edited: Apr 16, 2014
  6. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    For the sake of completeness, here's the complete solution:

    Code (csharp):
    1. /// <summary>
    2. /// Uses SetString()
    3. /// </summary>
    4. public static void SetDouble(string key, double value)
    5. {
    6.     PlayerPrefs.SetString(key, DoubleToString(value));
    7. }
    8. public static double GetDouble(string key, double defaultValue)
    9. {
    10.     string defaultVal = DoubleToString(defaultValue);
    11.     return StringToDouble(PlayerPrefs.GetString(key, defaultVal));
    12. }
    13. public static double GetDouble(string key)
    14. {
    15.     return GetDouble(key, 0d);
    16. }
    17.  
    18. private static string DoubleToString(double target)
    19. {
    20.     return target.ToString("R");
    21. }
    22. private static double StringToDouble(string target)
    23. {
    24.     if (string.IsNullOrEmpty(target))
    25.         return 0d;
    26.  
    27.     return double.Parse(target);
    28. }
    29.  
     
  7. shaderop

    shaderop

    Joined:
    Nov 24, 2010
    Posts:
    942
    You can also use BitConverter.DoubleToInt64Bits to get the actual bits representing the double, use bitwise shifting and masking to split the result into two ints, and store those in the PlayerPrefs.
     
  8. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Nah, not as good because that would require 2 prefs. Mine is just 1.

    EDIT: But I must admit that's a pretty creative solution :). Might try for the hell of it.
     
    Last edited: Apr 16, 2014
  9. shaderop

    shaderop

    Joined:
    Nov 24, 2010
    Posts:
    942
    Actual storage will be smaller in the general case, and I'm not aware that set pref calls are that expensive. But, hey, if you say so :)
     
  10. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Oh no no, both solutions are valid. No one is "right" or "better". Mine has it's pros and cons, and yours has its own pros and cons. Both are legitimate ways to solve the problem :)
     
    Pharaoh_ likes this.
  11. sarebots2

    sarebots2

    Joined:
    Jul 4, 2016
    Posts:
    34
    Actually, "R" specifier doesn't always return the same exact value. For double, use "G17" specifier.

    https://docs.microsoft.com/en-us/do...standard-numeric-format-strings#GFormatString