Help changing 0.100 to 0.1

So I have this timer for my speed run type game it has minutes seconds and milliseconds.

Right now this happens:
image

Here is the script:

local ConvertTime = {}

ConvertTime.Convert = function(Number)
	local min = math.floor(Number/60)
	local sec = math.floor(Number)-60*min
	local milisec = math.floor((Number-sec-min*60)*1000+0.5)

	if min < 1 then
		min = ""
	else
		min = min..":"
	end
	
	if sec < 10 then
		sec = "0"..sec
	end
	
	if milisec < 100 and milisec >= 10 then
		milisec = "0"..milisec
	elseif milisec < 10 then
		milisec = "00"..milisec
	end

	local output = min..sec.."."..milisec

	return output
end

return ConvertTime


I tried using math.clamp() but it didnt work.

Can you explain how it should work, what do you want to make or whats the error in your script so we can help you?

The title explains it.

In the image you can see 8.300 but I want it to be 8.3.

Other than that it works fine.

You could use

Num = string.format("%.1f", Num)

Although be aware it will round instead of truncate (Ex: 1.55 → 1.6)

if milisec >= 100 and math.floor(milisec / 100) == milisec / 100 then
	milisec = string.gsub(tostring(milisec),"0","")
elseif milisec >= 100 and math.floor(milisec / 10) == milisec / 10 then
	milisec = string.sub(tostring(milisec),1,2)
end
1 Like

Sorry, in hindsight I wasn’t looking closely at how you were formatting your numbers. You could try:

ConvertTime.Convert = function(Number)
	local min = math.floor(Number/60)
	local sec = math.floor(Number)-60*min
	local milisec = string.sub( tostring(math.floor((Number-sec-min*60)*1000+0.5)) , 1, 1)
	min = (min < 1) and "" or tostring(min) .. ':'
	local output = string.format("%s%02d.%s", min, sec, milisec)
	return output
end

I don’t like how milliseconds is handled in this but I can’t think of a better way to do it right now.

2 Likes