Timer - How do I show 2 digits?

Viewed 33

How do I make the timer text show 00:04 instead of 0:4? I tried Google but nothing worked for me. Thanks!

Timer.text = string.Format("Time Survived: {0:00}:{1:00}", Mathf.FloorToInt(Health.time / 60).ToString(), Mathf.FloorToInt(Health.time % 60).ToString());
1 Answers

When you use the "ToString()" method inside the string.Format(), you are passing a string to the formatter instead of a number, so it won't work.

You can change it to one of the options below in order to get it working:

//Option 1
Timer.text = string.Format("Time Survived: {0:00}:{1:00}", Mathf.FloorToInt(Health.time / 60f), Mathf.FloorToInt(Health.time % 60));

//Option 2
Timer.text = $"Time Survived: {Mathf.FloorToInt(Health.time / 60f):00}:{Mathf.FloorToInt(Health.time % 60):00}";

Both of them do the same thing.

Related