Formatting a string to display in a timer format

So I’m creating a little side project that uses RunService to countdown a client-sided timer. It starts at 60.00, then slowly counts down to 0.00.

Since RunService fires frames/sec, I’m subtracting this number by 1/frames to keep it consistently going down to 0. I’m not sure if this is the best solution, but I’m horribly unpracticed with things like tick(), which is probably a better solution.

Anyways, I want to format this resulting number in this manner:

local function Format(Number)
--- code that rounds decimal ending to two places plus adds zeroes for missing ones and tens place

-- prints 050.40

-- prints 127.87 

-- prints 009.23

This is probably doable with string.split and some rounding math - but then I’d have to concatenate the things together and add the zeroes when the number is below 100 and 10 and it’d get messy real fast. Any advice or posts to point me in the right direction? I’ve looked for this but I only found posts that formatted for h:m:s:ms and not [00]s:ms.

In order to format let’s say 5 to 005 you’d do this

local num = string.format("%0.3i", 5)
print(num) --> 005

But since this is only for integers, you’d have to split the number using string.split(Number, "."), format the first number and round the decimals.

1 Like

Here’s a simple function I made a while ago that’ll round the decimals. Num argument represents your number and places is for the amount of decimal places you want.

local roundDecimals = function(num, places)
    places = math.pow(10, places or 0)
    num = num * places
    if num >= 0 then 
        num = math.floor(num + 0.5) 
        num = math.ceil(num - 0.5) 
    return num / places
1 Like

Got it. Just used string.split to split the actual number and used “%.2s” to round the millisecond to two values. Thanks!

Alright, no problem!
[30 characters]

1 Like

What does “%0.3i” means. What does it do and how?

It takes an integer and sets it to a length of in this case 3.
You can find more about it here

1 Like

Oh thanks, I would like to learn more about this. Do you have useful links for this? I dont know what keyword to use for searching on google

Edit: thanks