I have this function which converts a number into a timestamp.
local function formatTime(t)
local minutes = math.floor(t / 60)
local seconds = math.floor(t) - minutes*60
local milliseconds = t - seconds - minutes*60
print(milliseconds, math.floor(milliseconds * 1000))
return ("%02d:%02d:%03d"):format(minutes, seconds, milliseconds * 1000)
end
However, I’ve noticed for certain numbers the timestamp is innaccurate.
print(formatTime(5.069)) -- 00:05:068
This may actually a bug with Lua, but I figured I’d post it here first in case I’m missing something obvious.
What I suspect is the root issue is that math.floor is run internally when string.format is used when using the d format specifier.
You’ll notice in the function I included the line: print(milliseconds, math.floor(milliseconds * 1000))
With the argument i plugged in I get 0.069 and 68 respectively which is incorrect as math.floor(0.069 * 1000) == 69.
Any help or suggestions would be greatly appreciated.
Hello, this is a known bug recognized by many others, this is currently being fixed, i’m not sure if it is to be fixed early or soon since the bug report was 5 months ago.
This does not seem to be the same bug. That is timestamps for the dev console which are generated by default when printing. These timestamps are generated by my own code.
local function formatTime(t)
local minutes = math.floor(t / 60)
local seconds = math.floor(t) - minutes*60
local milliseconds = t - seconds - minutes*60
local magicNumber = milliseconds* 1000
print('Is the magic number 69? ',magicNumber==69)
print("69 floored is ",math.floor(69))
print(magicNumber, math.floor(magicNumber))
return ("%02i:%02i:%03i"):format(minutes, seconds, milliseconds * 1000)
end
warn(formatTime(5.069)) -- 00:05:068
--[[
Is the magic number 69? false
69 floored is 69
69 68
00:05:068
]]
I’m small brained, but that number doesn’t seem to be 69 but rather some sort of decimal of some sort
local function formatTime(t)
local minutes = math.floor(t / 60)
local seconds = t % 60
local milliseconds = (seconds * 1000) % 1000
seconds = math.floor(seconds)
return string.format("%02i:%02i:%03i", minutes, seconds, milliseconds)
end
This is rather due to the internal formatting of lua (and many other languages). Automatic rounding is performed to represent the numbers in the best possible way. Milliseconds is actually less than 0.069