0

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.

Unity screenshot

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Alex
  • 43
  • 1
  • 11
  • 1
    `Math.Round` works with floats too – Panagiotis Kanavos Feb 13 '19 at 15:42
  • I am confused what is wrong with [`Mathf.Round`](https://docs.unity3d.com/ScriptReference/Mathf.Round.html) ? – Ali Kanat Feb 13 '19 at 15:43
  • https://stackoverflow.com/questions/8844674/how-to-round-to-the-nearest-whole-number-in-c-sharp – JamesS Feb 13 '19 at 15:43
  • 1
    You can take a look this topic https://stackoverflow.com/questions/8239969/rounding-of-float-values – Hien Nguyen Feb 13 '19 at 15:44
  • I'm not sure what you mean by "round with a variable". You never round a *variable*. You round a *value*. That value might be stored in a variable, but it's not the variable that is rounded. Can you explain what you mean? – Eric Lippert Feb 13 '19 at 15:53
  • I'm also confused why you say that floats are "easier to manipulate". Your question is "I'm having a hard time manipulating floats, but I want to keep using a float because they're easier to manipulate". Can you explain this contradiction? – Eric Lippert Feb 13 '19 at 15:54
  • Third, why are you using a float to represent time at all? Why not use TimeSpan, a type specifically designed to represent spans of time? Can you explain why you think that floats are the right way to represent time? – Eric Lippert Feb 13 '19 at 15:55
  • Possible duplicate of [How to Round to the nearest whole number in C#](https://stackoverflow.com/questions/8844674/how-to-round-to-the-nearest-whole-number-in-c-sharp) – Eliasar Feb 13 '19 at 15:56
  • @EricLippert sorry for the confusion let me explain. Firstly as to why I am using a float to represent time. I am new to C# and this is the first time I have used it so I am unfamiliar with all the aspects of it. Second as to what I meant by floats being easy to manipulate but having a hard time manipulating. I mean they are easier to manipulate than doubles and decimals as they are shorter but I was unaware how to do so in C#. Third, when I said round with variable I was able to round when I put a specific # in to math.round but not when I replaced this # with a variable that stored the #. – Alex Feb 14 '19 at 11:47
  • @AliKanat nothing is inherently wrong with it I just couldn't get it to work when I used a variable storing a value instead of just using the value, which worked fine. lack of experience on my part. – Alex Feb 14 '19 at 11:50
  • @Alex I was confused because you mentioned double and decimals because `Mathf.Floor` works with float and would solve your problem easier than other C# based solutions. – Ali Kanat Feb 14 '19 at 11:57
  • @Alex You can also take a look at [this](https://www.reddit.com/r/Unity3D/comments/45l4op/is_it_better_to_use_unity3ds_mathf_library_or_cs/) – Ali Kanat Feb 14 '19 at 11:59

2 Answers2

3

Since you're just concerned with the display of the float and not using the number for further calculations, you can just use the formatting features of the String class. For example,

timer.text = ": " + TimerValue.ToString("F2");

will round it and only display to 2 decimal places.

timer.text = ": " + TimerValue.ToString("F0");

will round it to a whole number.

Here's the documentation on the various formatting options available

Nanhydrin
  • 4,332
  • 2
  • 38
  • 51
2

You can just use string.Format to display the value with a set number of decimal places.

For example:

timer.text = string.Fomat(": {0:0}", TimerValue);    // format with 0 decimal places
// output
// : 118

timer.text = string.Fomat(": {0:0.00}", TimerValue);    // format with 2 decimal places
// output
// : 117.97

Note that this will round-up values.

haldo
  • 14,512
  • 5
  • 46
  • 52