Android - SharedPreference converting to Double

后端 未结 3 933
粉色の甜心
粉色の甜心 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:55

    You can parse the string as a double:

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    
    String weight =  sharedPreferences.getString("storednewweight", "");
    Double newweight = Double.parseDouble(weight);
    newweight = newweight + 5;
    
    //saves value into sharedpreference
    savePreferences("storednewweight", (Double.toString(newweight)));
    

提交回复
热议问题