In this case, I'm assuming that both can be used easily.
There are some cases, such as the following, where string.format can come in handy, because of it's ability to use to to make sure that the script puts in a 0 in the tens digit if the seconds are only at 8 or something. Are there any other cases or things string.format is really good for? Which is faster?
local function secondsToStamp(timeInSeconds) -- Converts 65 seconds to 01:05, see Lua string Library for more information
return string.format("%d:d", math.floor(timeInSeconds / 60), timeInSeconds % 60)
end
This function is the same, but a little bit messier:
local function secondsToStamp(timeInSeconds) -- Converts 65 seconds to 01:05, see Lua string Library for more information
return math.floor(timeInSeconds / 60) .. ":" .. (timeInSeconds % 60 >= 10 and "" or "0") .. (timeInSeconds % 60)
end
And even worse:
-- This one asks for a 2 digits at the beginning, which I decided I do not want for my newer script, but did not edit this because reasons
function secondsToStamp(seconds) -- Converts 65 seconds to 01:05
local mins = tostring(math.floor(seconds / 60)) -- seconds / 60 rounded down
local secs = tostring(math.floor(seconds % 60)) -- Remainder of seconds / 60
return (#mins < 2 and string.rep("0", 2 - #mins) or "") .. mins .. ":" .. (#secs < 2 and string.rep("0", 2 - #secs) or "") .. secs -- Return our string, while filling in the blanks so its 01:05 instead of 1:5
end
What are the other advantages of using string.format? Is there much of a difference?

3 comments
Thanks, but I still think string.format is a better alternative in this case.
I like this, haven't thought of it before. It's nice because it gives names to each of the fields
Yes, but if minutes was under 10, then you would need to add on an additional 0.