Android - SharedPreference converting to Double

后端 未结 3 938
粉色の甜心
粉色の甜心 2021-01-07 10:15

Basically i have a valued saved into shared preference as a string.

I am retrieving the value saved, and am trying to use it in a calculation.

How can i conv

3条回答
  •  终归单人心
    2021-01-07 10:53

    It can be saved as long like how I wrote it down:

    Double to Long:

    Double.doubleToRawLongBits(double);
    

    Long to Double:

    Double.longBitsToDouble(defaultLongValue);
    

    EDIT:

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    Long newweightTemp =  sharedPreferences.getLong("storednewweight", 0L);
    Double newWeight = Double.longBitsToDouble(newweightTemp );
    newweight = newweight + 5;
    //saves value into sharedpreference
    sharedPreferences.edit().putLong("storednewweight", Double.doubleToRawLongBits(newWeight)).apply();
    

    Something like that should do it.

提交回复
热议问题