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.
2 Answers
2
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
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:
http://wiki.unity3d.com/index.php/ArrayPrefs2
Or if you don't want to deal with exceptions, Decimal.TryParse().
– robertbu@robertbu 100% true.
– LandernHi, 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.
– OctoManIt's me again. I tested it out. void SaveAllData() { string mymoney = GMS.currentmoney.ToString (); PlayerPrefs.SetString("money", mymoney); } void LoadAllData() { GMS.currentmoney = decimal.Parse (PlayerPrefs.GetString("money")); } And it seem to work. Thanks alot
– OctoMan