Can someone explain this code for me?

Does anyone know what each part does and if it’s possible to optimize it?

local inputStr = tostring(textBoxTable.Text):gsub(" ", "")

if inputStr == "-" then
	textBoxTable.Text = "-"
elseif inputStr == "." then
	textBoxTable.Text = "."
end

local hasTrailingDot = inputStr:sub(-1) == "." -- checks if the last character is a period (.).

inputStr = inputStr:match("^[%d%.%-]*")

local dotCount = select(2, inputStr:gsub("%.", ""))
if dotCount > 1 then
	inputStr = inputStr:match("^[%d%.]+")
end

inputStr = inputStr:gsub("^(%-?)0+(%d)", "%1%2")
inputStr = inputStr:match("^%-?%d*%.?%d*")

if inputStr:find("%.") then
	local integerPart, decimalPart = inputStr:match("^(%-?%d*)%.(%d*)")
	if decimalPart and #decimalPart > 3 then
		decimalPart = decimalPart:sub(1, 3)
	end
	inputStr = integerPart .. (decimalPart and "." .. decimalPart or "")
end

if hasTrailingDot and not inputStr:find("%.") then
	inputStr = inputStr .. "."
end

textBoxTable.Text = inputStr

Also is it possible to only have 4 digits before the (.)?
So, the largest number would be 9999.999. Thanks!

Here i modified it a bit to do as said;


local inputStr = tostring(textBoxTable.Text):gsub(" ", "")

if inputStr == "-" or inputStr == "." then
    textBoxTable.Text = inputStr
    return
end

local hasTrailingDot = inputStr:sub(-1) == "."

-- Extract valid characters and limit to one dot
inputStr = inputStr:match("^[%d%.%-]*"):gsub("^(%-?)0+(%d)", "%1%2")
local dotCount = select(2, inputStr:gsub("%.", ""))
if dotCount > 1 then
    inputStr = inputStr:match("^[%d%.]+")
end

-- Extract the valid number format
inputStr = inputStr:match("^%-?%d*%.?%d*")

-- Limit digits before the decimal to 4
local integerPart, decimalPart = inputStr:match("^(%-?%d*)%.?(%d*)")
if integerPart and #integerPart > 4 then
    integerPart = integerPart:sub(1, 4)
end

-- Limit decimal places to three
if decimalPart and #decimalPart > 3 then
    decimalPart = decimalPart:sub(1, 3)
end

inputStr = integerPart .. (decimalPart and "." .. decimalPart or "")

if hasTrailingDot and not inputStr:find("%.") then
    inputStr = inputStr .. "."
end

textBoxTable.Text = inputStr

As for the code itself it ensures a numeric format.
First it converts the input into a string and using gsub replaces all the spaces with nothing essentially removing them.
If the input is just a single dot or - we skip further processing
We can check if it has a dot at the end by using :sub(-1) which will return the last character.
We then extract the valid characters and remove and leading zeros. We limit the dot count to 1. If there is more then 1 dot inputStr:match("^[%d%.]+") ensures that the first dot is only kept.
We then extract the valid format for the numbers.
We split inputStr into the integer part and the decimal part, if the integer part has more than 4 digits it truncates it to the first 4 digits if the decimal part has than 3 digits it truncated it to the first 3 digits.
We then reconstruct the input string.
If needed we restore the trailing dot.

And that’s it hopefully that helped :happy1:

That cleared a lot of things up :smiley:
However,

  1. I feel like “textBoxTable.Text = inputStr” on line 4 isn’t needed.
  2. The code is ran by :GetPropertyChangedSignal("Text"), so it runs every time I type something in the textbox. With that, I think the Extract valid characters and limit to one dot part could be shortened.
  3. You didn’t finish the “Limit decimal places to three”

I hope you can get these fixed, and thank you so much for the help so far!

1 Like

It seems somehow I on accident deleted somehow that part, here I now edited the post with the full part.

I never really knew the context of the code so I just modified it based of code snippet.

If you just type a number, it adds a dot at the end for some reason. Can you fix that? Thanks!