I have a basic for loop timer, only I'd like to present it to my String in the format of a digital clock I.e '0:00', how would I create the algorithm to Configure it to represent that way?
Cofiguring [i] into a Digital Format?
1 answer
for time = 1, 100 do
-- % (modulo) gets the remainder of a division
-- This gives the number of seconds
seconds = time % 60
-- If we take away the number of seconds, what we get is exactly divisible
-- by 60
remainingTime = time - seconds
minutes = remainingTime / 60
-- Format as a string
x = tostring(minutes) .. tostring(seconds)
end
This gives us almost what we want, but sometimes the time "1:5" is returned, rather than "1:05", for example.
--[[
Function to pad a string with 0s on the left to reach a minimum length
text: string -> The text to pad on the left with 0s
targetLength: integer -> The minimum length of the string we'll return
return -> Input text, left padded with 0s to a minimum of targetLength.
--]]
function zfill(text, targetLength)
-- Work out the number of digits required
digitsRequired = targetLength - string.len(text)
padding = string.rep("0", digitsRequired)
return padding .. text
end
That should work to achieve the padding we want.
Combining everything we get:
--[[
Function to pad a string with 0s on the left to reach a minimum length
text: string -> The text to pad on the left with 0s
targetLength: integer -> The minimum length of the string we'll return
return -> Input text, left padded with 0s to a minimum of targetLength.
--]]
function zfill(text, targetLength)
-- Work out the number of digits required
digitsRequired = targetLength - string.len(text)
padding = string.rep("0", digitsRequired)
return padding .. text
end
for time = 1, 100 do
-- % (modulo) gets the remainder of a division
-- This gives the number of seconds
seconds = time % 60
-- If we take away the number of seconds, what we get is exactly divisible
-- by 60
remainingTime = time - seconds
minutes = remainingTime / 60
-- Format as a string
minuteText = tostring(minutes)
secondText = zfill(tostring(seconds), 2)
print(minuteText .. ":" .. secondText)
end
11 comments
Could you explain the zfill function, and why call. it zfill?
zfill is just "zero fill". I stole the name from Python's in-built function which does the same thing.
It works by taking a bit of text and a target length. It tests the text's length. It needs to add (target length - actual length) zeros to get to the target length. string.rep just repeats a string a certain number of times, so string.rep("0", targetLength - actualLength) gives us the exact amount of padding we want. Then we just join everything together.
You should use the # length operator rather than string.len
What's the difference? I presume that one just calls the other under the hood.
There's just no reason to use len anymore with the length operator, plus it works with tables
But it's not a table, and will never be a table. You can't generalise out the function to left-pad any iterable, because string.rep is a necessary part. Your argument consists of "way A exists so don't use way B", but the exact same argument could work in reverse. There's just no reason to use "#" on strings when "string.len" exists.
In short, it smells like you're just finding faults for the sake of finding faults, rather than actually bringing anything constructive to the table.
name one occurrence in where len is preferable over # :\
Name one where # is preferable over len. They do the exact same thing. Unless you can provide me with some sort of manual page or benchmark showing that for some ridiculous reason one has worse asymptotic time complexity than the other, I fail to see any need to say one's better than the other.
You should use string.format like string.format("d", tostring(number)) instead of your zfill function. You can go to https://en.wikipedia.org/wiki/Printf_format_string for more information.
This discussion is preserved as it was. New answers, comments, and votes are closed.

8 comments
Include your attempt.
what he said
It's nothing but Label.Text = this until this = that then do this this way. I.e, it's not even an algorithm, I'm so lost, I need a complete explanation
unless you still want it
You use string patterns and simple math to convert seconds to what ever time. Know that a for loop is usually for counting to a point so it's more like a timer than a clock.
This question is trivial enough that as soon as you know how to do it (use mod), you've basically done it. Requesting an attempt for something somebody may have no idea how to even start is slightly unreasonable.
I know, I am using it to give the value to count down, duh. I just din't know how to format it into a constant algorithm, instead of having to use String patterns. Could you show me?
And he responded that all he had was basic code. I followed with a concept he could try.