Timer's tenths of seconds visible digits issue

I use a simple script for displaying the tenths of seconds on a timer. The following script is called GUITimerFragment.js and is applied to an empty game object I call GUI.

var startTime;

function Awake () {

    startTime = Time.time; 
}

function OnGUI () {

var runningTime = ((Time.time-startTime)*10)%10;

runningTime = String.Format ("{0:0}", runningTime);

GUI.Label(Rect(83,323,200,200), runningTime.ToString()); 

}

The issue is this: the timer begins counting the tenths of seconds normally: 1...2...3...4...5...6...7...8...9...10 only that I want it to display 0 instead of 10. I thought that the line

String.Format ("{0:0}", runningTime)

would make it show only one digit but it seems I'm wrong.

What I see is this (vimeo video)

What do I have to do to make it show "0" instead of "10"? - thanks :-)

It seems there was some strange thing going on where you were using the mod operator on a float. I cast this to an integer to floor the value.

Also String.Format ("{0:0}", runningTime); means to return runningTime without any decimals. It doesn't constrain the number to one digit.

var startTime : float;

function Awake () 
{
    startTime = Time.time; 
}

function OnGUI () 
{
    var runningTime : int = ((Time.time - startTime) * 10) % 10;
    var runningTimeText : String = String.Format ("{0:0}", runningTime);
    GUI.Label(Rect(83,323,200,200), runningTimeText); 
}

Here is another way of achieving the same result:

import System;

var startTime : float;
var rect = Rect ( 83, 323, 200, 200 );

function Awake () 
{
    startTime = Time.time; 
}

function OnGUI () 
{
    var span = TimeSpan.FromSeconds(Time.time - startTime);
    var tenths = span.Milliseconds / 100;

    GUI.Label ( rect, tenths.ToString() ); 
}

See this page. Unity has a built-in function for working with modulo and floats

For formatting elapsed time for GUI display I use this method ... easily tweakable if you want tenths, or hours or other variations.

public string FormatTime(float time)
{
    int d = (int)(time * 100.0f);
    int minutes = d / (60 * 100);
    int seconds = (d % (60 * 100)) / 100;
    int hundredths = d % 100;
    return String.Format("{0:00}:{1:00}.{2:00}", minutes, seconds, hundredths);
}