- Home /
Save decimals in PlayerPrefs c#
I was wondering how to save decimals in the playerPrefs. Usually in use
public decimal current_money = 12345678901234567890M;
PlayerPrefs.SetFloat ("current_money", GMS.current_money);
Do i have to convert it into a string to save it?
stored_money = current_money.toString();
Like so? Or are there other ways to do it? Thanks in advance.
Answer by Landern · Oct 12, 2014 at 03:51 PM
Save it as a string so no precision is lost by converting to another type.
On load, get the preference and use Decimal.Parse or Convert.ToDecimal
Or if you don't want to deal with exceptions, Decimal.TryParse().
Hi,
thanks for your answers. I forgot about the loading part so thanks to clear that. So when loading i convert the string back first and send it back to currentmoney right?
I'll give that a try.
It's me again.
I tested it out.
void SaveAllData()
{
string mymoney = G$$anonymous$$S.currentmoney.ToString ();
PlayerPrefs.SetString("money", mymoney);
}
void LoadAllData()
{
G$$anonymous$$S.currentmoney = decimal.Parse (PlayerPrefs.GetString("money"));
}
And it seem to work. Thanks alot
Answer by robertbu · Oct 12, 2014 at 03:51 PM
As far as I can see you are stuck with:
Saving it as a string an converting it back on read
Saving it as a float and converting it back on read
Saving it as two integers and reassembling
Saving it as binary and converting it back
For the last one, there have been a couple of posts on UA, plus boolean values are saved in a binary format in the following script on the Unity Wiki: