How to keep 2 decimal places at all times?

  1. What do you want to achieve? Keep it simple and clear!
    I am making an in-game time system and I need the time value to be displayed with 2 decimal places at all times, even when its not needed.
  2. What is the issue? Include screenshots / videos if possible!
    I do not know how to keep the 2 decimal places. For example, when the time changes to 9:40, the 0 at the end of 9:40 gets removed automatically.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I searched far and wide, and found no solution.

Thanks in advance!

1 Like

you can easily get a time like this using a function and telling it how many seconds you have

It is still the same time it is just automatic Roblox

function ChangeTime(Seconds)
    --code
    return Time
end

ChangeTime(Seconds)

print(ChangeTime(100)) --would be an output of 1:40
1 Like

note that this is an example on what you could do

Wouldn’t that just make the seconds parameter 100? it wouldnt actually change the decimal places

what exactly do you mean? im just saying functions could be used

But how do i actually keep the zeros

print(ChangeTime(64)) --would be an output of 1:04
it would keep it at two places

easy
if you want I could make a function?

Simple, you don’t use decimals. You store the time as integers in seconds then format string from that instead:

local Time = 360
local TextLabel = script.Parent
while Time > 0 do
    wait(1)
    Time -= 1
    TextLabel.Text = string.format("%c:%c", math.floor(Time / 60), Time % 60)
end

yes please make a function because i am a bit confused with what ur saying

yea but you should use a function so you could do it multiple times with less code

would you mind explaining how your code works?

yea i think you are right, a function would be better

local TextLabel = script.Parent
function ChangeTime(Time)
    local FullTime = 0
    while Time > 0 do
        wait(1)
        Time -= 1
        FullTime = string.format("%c:%c", math.floor(Time / 60), Time % 60)
    end
    return FullTime
end

print(ChangeTime(100)) --1:40
print(ChangeTime(64)) --1:04

TextLabel.Text = ChangeTime(420)

I modified ben’s script so yea

I just noticed there is a wait in there so you would have to change it a bit

Sure, you have a variable named Time that stores the remaining game time and you have another variable named TextLabel that hold reference to the instace that script.Parent property holds, in this case it’s TextLabel instance.

Then you have a while loop that loops until Time variable is bigger than 0.
In every loop cycle code yields for a second with wait(1) then aubtracts the value Time hold by 1, then it updates the TextLabel’s Text propert using string.format().
string.format’s first argument is the string pattern with 2 %c flags in it. %c is used for converting given numbers (as integers) to string and place it inside string by replacing the %c flag with the given integer. The next 2 arguments are minutes and seconds passed as arguments to string.format to replace the 2 %c flags respectively.

I didn’t mean game time in that way, I meant like an in-game clock.