Sun
Sun

Reputation: 6888

The method getLong(String, long) in the type SharedPreferences

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

Answers (4)

Hussein El Feky
Hussein El Feky

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

skrzatswat
skrzatswat

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

Adeel Ahmad
Adeel Ahmad

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

Huzefa Gadi
Huzefa Gadi

Reputation: 1149

long cannot be null, u can use

e1 = pref.getLong("key_grand_total", 0.0);

Upvotes: 1

Related Questions