HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Converting seconds to mm:ss

11-09-2006, 08:14 AM#1
Moss
Is there a custom function out there for converting a bunch of seconds into a formatted time string like mm:ss? Or could somebody quickly demonstrate how to do it? I tried to be cool and make one myself but I am stuck on getting the minutes and second to display like "15:05" instead of "15.0: 5.0".
11-09-2006, 09:53 AM#2
Jazradel
Convert those to integers before displaying them?
11-09-2006, 10:07 AM#3
blu_da_noob
Collapse JASS:
function FormatTime takes integer seconds returns string
    local integer hour = seconds/3600
    local integer minute = (seconds-hour*3600)/60
    local integer second = seconds-(seconds/60)*60
    local string s = ""
    
    if hour > 0 then
        set s = I2S(hour)+":"
    endif    
    if minute >= 0 then
        if minute < 10 and minute > 0 then
            set s = s + "0" + I2S(minute) + ":"
        else
            set s = s + I2S(minute) + ":"
        endif
    endif
    if second < 10 then
        set s = s + "0" + I2S(second)
    else
        set s = s + I2S(second)
    endif
    return s
endfunction

Something like that should do it...
11-10-2006, 08:06 AM#4
Moss
Duh (on myself), I was converting it to integers first but then I removed it in an attempt to fix other problems. And I was trying to convert Real > Integer > String at every step instead of dealing with an int right off the bat. Thanks blu_da_noob, I will use your function because it works. I just had to add a bit to make sure it did 1:00:59 instead of 1:0:59. That is interesting how dividing the integers works, I wasn't sure how that would work and at first I thought your function couldn't work at all. I guess dividing with integers simply chops off the decimal, no rounding. Pretty cool, you didn't even need to use modulo like I was using.
11-10-2006, 11:08 AM#5
blu_da_noob
Integer division rounds down, but real division keeps the decimal. So:
Collapse JASS:
local integer i
local real r
// i / r -> decimal
// i / 2 -> rounds
// i / 2. -> decimal (note the period after the 2, that makes it a real)
// r / 2 -> decimal (you have one real already, so it becomes real division)
// summary: if atleast one of your two inputs in a division is real, the output will be real, if both are integer, it will be a rounded (down) integer
11-12-2006, 01:34 PM#6
Thunder_Eye
Quote:
Originally Posted by blu_da_noob
Collapse JASS:
    local integer second = seconds-(seconds/60)*60
If I remember my school math correct * and / are calculated before + and -
So if seconds = 30
30-(30/60)*60
30-0.5*60
30-30
= 0
?
11-12-2006, 01:37 PM#7
blu_da_noob
Integer division.
30-(30/60)*60
30-0*60 (30/60 is rounded down to 0 in integer division)
30-0
= 30
11-12-2006, 01:40 PM#8
Thunder_Eye
oh right integers ^^
I hate those little bastards
11-12-2006, 01:47 PM#9
Captain Griffen
You prefer floating point values?

Integers are WAY more simple.
11-14-2006, 02:28 PM#10
Waldbaer
...and even more accurate in some cases!