How could i fill in blank decimal spaces with a 0?

I’m trying to do a time display gui, but there’s one issue, it’s cutting off at the last decimal with a non-zero number in it, and i want it to always take up 3 decimal spaces, how could i go about doing this?

3 Likes

you could use some string manipulation to achieve this

local exampleTime = 0.3
local split = tostring(exampleTime):split('.')

local length = split[2]:len()
if length < 3 then
  for i = 1, (3 - length), 1 do
    split[2] = split[2] .. 0
  end
end

local finalTime = split[1] .. '.' .. split[2]
print(finalTime)

the outcome of this should be the string “0.300” printing

after reading the reply below, you could alternatively use string.format which is something i completely forgot about

2 Likes

You can use a formatting pattern:

string.format("%.3f", number)
  • % signifies the start of the pattern
  • .3f specifies three significant figures
4 Likes

i see, thanks for the help anyways!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.