- Home /
Formatting a ToString for time
Hi, I'm trying to make a digital clock in C# and I am having trouble with the formatting. I am trying to get a countdown from 5 min and display it in a mm:ss format. Here is what I have so far, but I can't figure out how to adjust the formatting in the ToString.
currentTime = nextDay - Time.time;
min = Mathf.Ceil(currentTime/60);
sec = Mathf.Ceil (currentTime % 60);
var timer = min + ":" + sec;
GUI.Label (new Rect(712,495,50,30), timer.ToString());
The "timer" variable is already a string, so you don't use ToString there since it does nothing. What are you actually trying to do?
Answer by zyzyx · Oct 02, 2012 at 04:56 PM
You can use the C# String.Format Method. This could look something like this (for mm:ss format):
public static string SecondsToString(int sec)
{
int minutes = sec / 60;
int seconds = sec % 60;
return string.Format("{0:00}:{1:00}", minutes, seconds);
}
Answer by Broheim · Oct 02, 2012 at 04:32 PM
min = Mathf.Ceil(currentTime/60);
min_s = min.ToString();
while (min_s.Length < 2)
{
min_s = "0" + min_s;
}
Didn't test it.
Your answer
![](https://koobas.hobune.stream/wayback/20220613081753im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Bullet Points 1 Answer
Masked InputField on Unity UI 4 Answers
Change the content of a Text Mesh 1 Answer
Indentation formatting issue in MonoDevelop-Unity 4.0.1 (Win7) 3 Answers
Where should I put this function? 2 Answers