Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How can I format the textfield that shows milliseconds to only show 2 digits?

Discussion in 'Scripting' started by scr33ner, Jan 4, 2023.

  1. scr33ner

    scr33ner

    Joined:
    May 15, 2012
    Posts:
    187
    Here is the code I'm using:

    Code (CSharp):
    1. private void SetTimerText()
    2.     {
    3.      
    4.         float minutes = Mathf.FloorToInt(raceTimer / 60);
    5.         float seconds = Mathf.FloorToInt(raceTimer % 60);
    6.         float milliSeconds = Mathf.FloorToInt((raceTimer - seconds) * 1000);
    7.  
    8.         txtTimerMin.text = minutes.ToString("00");
    9.         txtTimerSec.text = seconds.ToString("00");
    10.         txtTimerMilliSec.text = milliSeconds.ToString("00");
    11.  
    12.     }
    the milliseconds just keeps going.

    I've searched but haven't found anything useful.
     
  2. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    717
    Do you want the timer to look something like this?

    3 minutes, 32 seconds, 150 milliseconds
    03:32:15

    1 minute, 4 seconds, 10 milliseconds
    01:04:01

    If so, just multiply by 100, not 1000 :p. You might still need to round down, so that 996 ms becomes 99, not 100.
     
    scr33ner and lordofduct like this.
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    As @chemicalcrux has said, you've multipled by 1000, which will definitely give you 3 digits of precision. You should be multiplying by 100 instead if you only want 2 digits. And note... these wouldn't be 'milliseconds' anymore, it'd be centiseconds (milli means thousandths, which is 3 digits. centi means hundredths, which is 2 digits).

    BUT... there's one other issue, you're subtracting seconds from raceTime, but your seconds is modulo'd over 60. This means seconds is always constrained to 0->60, while raceTimer goes on forever (unconstrained). If you do what chemicalcrux suggested it'll work until you hit the 1 minute mark. Then it'll fail all over again for a completely different reason.

    If you want centiseconds do:

    Code (csharp):
    1. float centiSeconds = Mathf.FloorToInt((raceTimer % 1f) * 100);
    2. //...
    3. txtTimeMilliSec.text = centiSeconds.ToString("00");
    (raceTimer % 1f) results in only the fractional portion of your timer, and assuming raceTimer is in seconds, this would be the fractional portion of the seconds. You then just multiply it by the amount of precision you want and floor off the rest.

    Alternatively you could house this all in one single text field and use format strings:
    Code (csharp):
    1. var ts = System.TimeSpan.FromSeconds(raceTimer);
    2. txt_Output.text = ts.ToString("mm\\:ss\\:ff");
    or:
    Code (csharp):
    1. var ts = System.TimeSpan.FromSeconds(raceTimer);
    2. txt_Output.text = $"Race Timer: {ts:mm\\:ss\\:ff}";
    The slashes are to escape the colon, colon means something in format strings.

    And the 2nd option allows putting other text into the string easily. Note my example will display it not just with the time, but with the prefix "Race Timer: ##:##:##".
     
    Last edited: Jan 4, 2023
    scr33ner likes this.
  4. scr33ner

    scr33ner

    Joined:
    May 15, 2012
    Posts:
    187
    Thanks @lordofduct, this is perfect.

    I'd have loved to use a single text field but it made the timer jerky with every digit change.
     
  5. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    717
    A monospaced font would fix that!
     
    lordofduct likes this.
  6. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    You can also force any font monospace with the <mspace> tag in TextMeshPro:
    https://docs.unity3d.com/Packages/com.unity.textmeshpro@3.2/manual/RichTextMonospace.html

    Though I'll say it's a little finicky at times. Depending the font and value put in, the monospace sometimes still has a small jiggle. You'll just have to play with it based on your font.

    Or... just get a monospace font.

    (this is literally a problem I had to deal with yesterday in the current project I'm working on)
     
    scr33ner and oscarAbraham like this.
  7. scr33ner

    scr33ner

    Joined:
    May 15, 2012
    Posts:
    187