I recently wrote a small program where I'm trying to convert a number of seconds into a more human readable format, but I'm having problems when trying to format the output and somehow I end up getting unexpected output. Here is the code
local minutes, hours = 60, 3600
local s = 5400
-- output is 2h30m when it should be 1h30m
if s >= hours then
print(string.format("%.0fh%.0fm", s / hours, (s % hours) / minutes))
end
-- output is 45m0s when it should be 45m
if s >= minutes then
print(string.format("%.0fm%.0fs", s / minutes, s % minutes))
end
Even though the amount of seconds (s) is 5400 which is 1 hour and 30 minutes, the first if
-statement will ouput 2h30m which is one hour more, whereas the second if
-statement prints the correct 90m. If y'all could help me with some ideas or point me in the right direction, I'd be extremely grateful! Have a good one!
os.date()
for this? Like:print(os.date("!%Hh %Mm %Ss", 5400))
(printing:01h 30m 00s
)