Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Time converting problem?

Asked by 8 years ago

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!

0
Have you tried %d instead of just d? GoldenPhysics 474 — 8y
1
Yeah - See my "NOTE" :P jjwood1600 215 — 8y
0
Have you tried "%d.2" instead of ""d"? GoldenPhysics 474 — 8y
0
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 :/ jjwood1600 215 — 8y

1 answer

Log in to vote
1
Answered by 8 years ago

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
0
Thanks!! jjwood1600 215 — 8y
Ad

Answer this question