Trunciating to 0dp

Hi :slight_smile:
How do i remove all dp after a number? rounding seconds to β€œ55” rather than β€œ55.43”
Currently using something like

 if(Timer > 0){
  Timer -= Time.deltaTime;
  Timer = Mathf.Round(Timer * 100.0f) / 100.0f;
}

Full code -

var Timer : float = 60.0;
var TextStyle = new GUIStyle();
//var  Drop = 6;

function Update() {

 if(Timer > 0){
  Timer -= Time.deltaTime;
  Timer = Mathf.Round(Timer * 100.0f) / 100.0f;
 
 
}

 if(Timer <= 0){
  Debug.Log("Dropped");
 }
}

function OnGUI(){
 GUI.Label( Rect(Screen.width -500, 40, 200, 30), "Drop in:" + Timer, TextStyle);

}

and using 0.0f was giving me the good old NaN error for diving by zero.

Thanks
P.S - sorry if a repost, couldn’t see after a quick google

To make the coutdown work you need to subtract fractions of 1 since you execute the code several times a sec. Time.deltaTime is a quite small number (at 60fps it’s 1/60 ~0,0166). In your code you subtract this small amount, but Mathf.Round will round up to the next whole number Mathf.Round(54,983) == 55.

Don’t round the variable. It’s function is to count down. Just round the the number where you want to display it:

GUI.Label( Rect(Screen.width -500, 40, 200, 30), "Drop in:" + Mathf.Round(Timer), TextStyle);

It should also work by using a ToString format like this:

GUI.Label( Rect(Screen.width -500, 40, 200, 30), "Drop in:" + Timer.ToString("00"), TextStyle);

which will also add a leading 0