I am creating a timer for a game in C#:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; // key to ensuring this works. interfaces with the ui.
public class Timer2 : MonoBehaviour
{
public static float TimerValue = 120; // initial timer value
Text timer;
// Use this for initialization
void Start()
{
timer = GetComponent<Text>();
}
// Update is called once per frame
void Update()
{
timer.text = ": " + TimerValue;
TimerValue = TimerValue - Time.deltaTime;
}
}
The problem with this solution to a timer is that it displays the timer as a float (see picture below). while technically functional it looks really bad and the numbers move side to side (due to the different widths of the numbers) making it hard to read.
How can I round this number so it can be displayed as a whole number? I looked around and only found rounding for the double and decimal data types. I also couldn't figure out how to round with a variable as all the examples I tried didn't work with variable. Ideally I would like to keep using float as it is easier to manipulate and I don't need the detail of a decimal ro double.