Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Time converting problem?

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!

4 comments

0

Have you tried %d instead of just d?

1

Yeah - See my "NOTE" :P

0

Have you tried "%d.2" instead of ""d"?

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 :/

1 answer

✓ Accepted answer

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

1 comment

0

Thanks!!

This discussion is preserved as it was. New answers, comments, and votes are closed.