Float Number will not format correctly

Basically what im doing is changing the name of my tool every 0.1 seconds but sometimes it does 0.099999 (for a large amount of times) which gets annoying since it isnt visually appealing

I tried many solutions such as Clamping, Formatting using string.format and dividing then multiplying but it still doesnt want to work so uhh please help

local function Format(Number: number): string
	local formatted = string.format("%.3f", Number)
	formatted = formatted:gsub("%.?0+$", "")
	return formatted
end

function ToolModule:Debounce(Tool: Tool, Time: number, Name: string)
	assert(Tool, "Invalid Tool")
	Time = Time or 2
	assert(Name, "Invalid Name")
	local Elapsed = 0
	local Interval = 0.1
	
	while task.wait(Interval) do
		Tool.Name = Time - Elapsed
		Elapsed += Format(Interval)
		print(Elapsed)
		
		if Elapsed >= Time then
			Tool.Name = Name
			break
		end
	end
end

^^
this is my current script

(i also tried using runservice with dt instead of a while loop but for some reason rounding up dt to a min of 0.001 will just not work ? idk, i gave up on that)

i actually did read your post and i tried it, yet it didnt work, so ill try again though if it does work ill be sure to mark as the solution (ill edit this message once i test it)
thanks!

1 Like

Deleted the post because there’s a much better solution:
I don’t have the time at the moment to put it in your code but I’m sure you’re talented enough to! Here’s the code:

string.format("%.1f", 1.32341)) -- 1.3 as number

%.1 = Nearest 0.1
%.2 = Nearest 0.01
etc.

Hope this helps :smiley:

i see, but isnt that the same as what i originally had " string.format("%.3f", Number) " ?

1 Like

Just tested it and just printing elapsed (Line 13s) will give that 1.099999999999.

Doing this (shown in line 14 in console) never gives that weird number
print(string.format("%.1f", Elapsed))

So I think you should do
Tool.Name = string.format("%.1f", Time - Elapsed)

Let me know if it doesn’t work!

instead of adding the corrected value, just change the name to the correct value

the weird floating point number is unavoidable unless you use more data. it doesnt get fixed when you add the rounded number

i see, thanks! i didnt know it was that simple lol

1 Like

yes but the elapsed value needs the original value and the added time, hence the name elapsed

i actually tried it and it worked at first but now it doesnt really work anymore for some reason…

Floating-point precision error.

local function Format(Number: number): number
	return math.floor(Number * 1000 + 0.5) / 1000
end

function ToolModule:Debounce(Tool: Tool, Time: number, Name: string)
	assert(Tool, "Invalid Tool")
	Time = Time or 2
	assert(Name, "Invalid Name")
	local Elapsed = 0
	local Interval = 0.1
	
	while task.wait(Interval) do
		Elapsed = Format(Elapsed + Interval)
		Tool.Name = tostring(Time - Elapsed)
		print(Elapsed)
		
		if Elapsed >= Time then
			Tool.Name = Name
			break
		end
	end
end

i already tried dividing and multiplying, it still doesnt work

okay so i decided to remake my entire script for the debounce handling and i used a different approach, instead of incrementaly adding numbers which may cause floating point errors, i recalculate it each time and assign the value after that, and by using os.clock(), it reduces the risk even more so

heres my final script for the people who are interested in fixing a similar error!

function ToolModule:Debounce(Tool: Tool, Time: number, Name: string)
	assert(Tool, "Invalid Tool")
	Time = Time or 2
	assert(Name, "Invalid Name")
	local Elapsed = 0
	local Interval = 0.1
	local Start = os.clock()
	
	Tool.Name = Time
	
	while task.wait(Interval) do
		local Elapsed = os.clock() - Start
		local Remaining = math.max(Time - Elapsed, 0)
		Remaining = math.floor(Remaining * 10 + 0.5) / 10
		Tool.Name = Remaining
		print(Elapsed)
		
		if Elapsed >= Time then
			Tool.Name = Name
			break
		end
	end
end