- Home /
How do you save decimals with PlayerPrefs using XML???
I'm trying to store the number of bitcoins players have in my game, so its a lot of decimals (0.000000). I don't know if I need to split the decimal into 2 ints then put it back together or if you can simply save it using a different method. I am saving it using XML.
public class SaveState
{
public decimal bitcoin = Click.btcoin;
}
Answer by zoran404 · Mar 07, 2018 at 09:13 AM
You can't save decimal
numbers.
So instead you will have to save it as a string.
Use decimal.tryParse()
and myDecimal.toString()
You can use decimal.parse()
if you are sure you will not get an exception.
Would you be able to tell me how to put it, my decimal value is Click.btcoin. its probably way off but I tried:
public string bitcoin = Click.btcoin.ToString();
PS. I'm very new to coding.
Answer by darkherodnz · Mar 07, 2018 at 08:32 PM
You can writer a wrapper:
public static class PlayerPrefsWrapper
{
public static void SetDecimal(string key, decimal value)
{
PlayerPrefs.SetString(key, x.ToString());
}
public static decimal GetDecimal(string key)
{
decimal value;
return PlayerPrefs.HasKey(key) && Decimal.TryParse(PlayerPrefs.GetString(key), out value) ? value : 0;
}
}
then in your savestate class you just have to do:
Set: PlayerPrefsWrapper.SetDecimal("bitcoin", bitcoin);
Get: bitcoin = PlayerPrefsWrapper.GetDecimal("bitcoin");
Sorry! Idk why i put double there :O, i've fixed it, thanks!
Your answer
![](https://koobas.hobune.stream/wayback/20220612154100im_/https://answers.unity.com/themes/thub/images/avi.jpg)