Hi guys!
function DigitalTime(TimeNum) local Time = math.max(0, TimeNum) local minutes = math.floor(Time / 60) % 60 local seconds = math.floor(Time) % 60 local m = math.floor(Time) local Timestring = string.format("d:d:d", minutes, seconds, m) return Timestring end print(DigitalTime(16.11231231238))
NOTE: d:d:d should actually be *02d:02d:03d" with a percentage sign before each of these - for some reason the website wouldn't let me put it like that :/
I have this function which converts this time to: 00:16:016
Any ideas why it is converts it to "016" when it should actually be "112"?
I have tried many different variations of this script and still get the same conversion :(
Is there a way to fix it and still keep the 3 numbers after the decimal place?
Any help appreciated! Thanks!
The reason that section of the string is "016" because of your use of math.floor
on line 5. This is just removing the fractional part of your Time
variable. We could use this a bit differently to just give us the fractional part instead:
local Time = 16.11231231238 --local m = math.floor(Time) -- 16 local m = Time - math.floor(Time) -- 0.11231231238
Now you'll need the fractional part to be the first three digits for formatting:
local Time = 16.11231231238 --local m = Time - math.floor(Time) -- 0.11231231238 local m = math.floor((Time - math.floor(Time))*1000) -- 112