Ad
Log in to vote
1

What formula can print seconds into minutes:seconds?

Asked by
Zerio920 285 Moderation Voter
7 years ago

Using this for a timer I'm making.

So say for example, I have a script read a number from a value object, holding an amount of seconds. Then the script would print in the output the amount of minutes:seconds that would equal. I'm having trouble thinking of something that can do that. Help is appreciated.

1 answer

Log in to vote
2
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
7 years ago

I just wrote one earlier today, actually.

Seconds/60 == minutes. Chop of the fractional part with math.floor and you have your minutes. Get the remainder of seconds using Seconds%60.

For formatting, you have to add in an extra '0' when the minutes or seconds are less than 10:

local time = secondsValueObject.Value --By the way, I suggest using an IntValue as opposed to a NumberValue for this.

local seconds, minutes = time%60, math.floor(time/60)

local timerDisplayText = (minutes < 10 and "0" or "") .. minutes .. ":" .. (seconds < 10 and "0" or "") .. seconds
0
Thanks! Really saved a lot of time ;) Zerio920 285 — 7y
0
You could make this better by using 'string.format("%.2d", seconds)' - that way you turn your 'timerDisplayText' into something like this: digpoe 65 — 7y
0
Whoops. Forgot comments can only be one line. Like I was saying, you could turn it into: local timerDisplayText = string.format("%.2d:%.2d", math.floor(time/60), time%60) digpoe 65 — 7y
1
String patterns are beyond me in all honesty. adark 5487 — 7y
Ad

Answer this question