Reputation: 6888
I have a Double variable
Double doubleGrandTotal = 500.00;
I am storing it in SharedPreferences like this:
editor.putFloat("key_grand_total", Double.doubleToLongBits(doubleGrandTotal));
editor.commit();
But, I don't know How can I get this value:
double e1 = 0.0;
e1 = pref.getLong("key_grand_total", null);
I am getting The method getLong(String, long) in the type SharedPreferences is not applicable for the arguments (String, null)
Upvotes: 1
Views: 176
Reputation: 6707
An easier solution I always do is save the double as a string:
editor.putString("key_grand_total", String.valueOf(doubleGrandTotal));
editor.commit();
And retrieve it as:
double e1 = 0.0;
e1 = Double.parseDouble(pref.getString("key_grand_total", "0"));
Upvotes: 1
Reputation: 59
you should convert long to double to read it from prefs correctly also you should set default value for long, can't be null:
double e1 = 0.0;
e1 = Double.longBitsToDouble(pref.getLong("key_grand_total", 0));
Upvotes: 1
Reputation: 969
try
double e1 = 0.0;
e1 = pref.getFloat("key_grand_total", 0);
second argument is the default value, in case key_grad_total
is not found in pref, default value is returned to e1
Upvotes: 1
Reputation: 1149
long cannot be null, u can use
e1 = pref.getLong("key_grand_total", 0.0);
Upvotes: 1