can you turn a simple int to a decimal. EG 34 to 0.34

hey gang since my Text gives me a hole number (as a test it gives me 34 )
I need to make my Fillamout be 0.34 or 34 gives me a full bar rather then a slightly full bar.

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class XPbarcontroll : MonoBehaviour {

	public Text XP;
	public Image XPBar;
	public int XPAmount;

	// Update is called once per frame
	void LateUpdate () {
		int.TryParse(XP.text, out XPAmount);   // returns 34. needs to be turned into 0.34
		XPBar.fillAmount = XPAmount;    

	}
}
  • int is a whole number
  • float is number with desimals that stores 32bits of information
  • double is same as float but with 64bits (double precision)
  • decimal is same as float and double but with 128bits

For this float is probably more than enough so you can do it like this:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class XPbarcontroll : MonoBehaviour
{
    public Text XP;
    public Image XPBar;
    public int XPAmount;

    void LateUpdate ()
	{
        if(int.TryParse(XP.text, out XPAmount))
		{
		    XPBar.fillAmount = (float) XPAmount / 100f;
		}
		else
		{
		    print("Couldn't parse XP: " + XP.text + " as integer!");
		}
    }
}

I added if to check if the tryparse even succeeds to prevent strange errors if for some reason it cannot parse int out of XP.text. (float) is called typecasting which means that we change the result to float. And simply to get 0.34 from 34 we can just divide it by 100f and that f after 100 means that the number is float.

XPBar.fillAmount =( XPAmount / 100);