Wednesday, 28 August 2013

Correcly saving the double in the Shared preferences

From the basic method that includes in the Shared preferences there is not specific method for double (setter and getter), but for Float. The problem is in Java, float has 32 bits and double 64, which if we set and get the double as float, there could be problem of precision.[1]

In order to save double correctly in the shared preferences, it must be saved as long, which also has 64 bits.[1]

To do so, double must be converted (not casted) to Long.

- Double to Long:

- Long to Double

[1]: The Java tutorials - Primitive Data Types
[2]: StackOverflow - Cant put double SharedPreferences