get minutes and seconds for a digital clock out of a single number?

I have an integer of total time played for my game, but it’s just the total seconds, and I’d like it to come out in minutes and seconds… so like 94 would be 1:34

What I’m currently doing…

//lets call x my total second int

int minutes = Mathf.RoundToInt((x / 60) - 0.5f); //I subtract 0.5 to make sure I always round down 
int seconds = x - (minutes * 60);

It works for the most part, but for some reason I get negative numbers if I have lower numbers (I think 10 or under but not sure)

Is there a better way to do it?

Hmm… Maybe;

http://unity3d.com/support/documentation/ScriptReference/Mathf.Floor.html

What I would do:

private var timeTestVar :int =12312;

function Start(){
Time.timeScale *=10;
}

function Update () {
var x :float = Mathf.Floor(Time.time);
var seconds: int = x % 60;
var minutes:int = (x-seconds)/60;

//update when the second has changed
if (!(timeTestVar == seconds)){
timeTestVar = seconds;
print( minutes + ":" + seconds);
}
}
1 Like