Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do I turn seconds to minutes and seconds?

Asked by 8 years ago

Basically let's say I have a number, N. I want to do a function DecimalsToMinutes, that converts N to Minutes and Seconds(MS). For example if N is 192, it turns it into "3:12".

This is how I'm thinking it would look:

1function DecimalsToMinutes(n)
2    local ms = --do math with n
3    return ms
4end

The problem is that I don't know how to do it, so do you?

1 answer

Log in to vote
1
Answered by 8 years ago
Edited 8 years ago

Yes, I do! Simply use a divider to find the number of minutes, and the modulo operation to find the number of seconds

1function DecimalsToMinutes(dec)
2    local ms = tonumber(dec)
3    return math.floor(ms / 60)..":"..(ms % 60)
4end
5 
6print(DecimalsToMinutes(192)) --Prints 3:12

Now I dont know to what extent this will work but hopefully it will work for every situation!

Hope this helped!

0
Extra Info here: The Modulo Operand gets the remainder of the division. Example: 512 / 60 = 8.53, 512 % 60 = 32 httpOmqCxpcake 70 — 8y
0
wow! thanks! wookey12 174 — 8y
0
It converts 9 seconds to 0:9 seconds though, but it works! Thanks! AwesomeGamer3894 0 — 4y
Ad

Answer this question