Is there a way to do this with textboxes?
Input | Desired Outcome | |
---|---|---|
5 | → | 5 |
5. | → | 5. |
5.a | → | 5. |
5.6789 | → | 5.678 |
5.00000000 | → | 5.000 |
- | → | - |
. | → | 0. |
-. | → | -0. |
Thanks!
Is there a way to do this with textboxes?
Input | Desired Outcome | |
---|---|---|
5 | → | 5 |
5. | → | 5. |
5.a | → | 5. |
5.6789 | → | 5.678 |
5.00000000 | → | 5.000 |
- | → | - |
. | → | 0. |
-. | → | -0. |
Thanks!
function formatInput(input)
local inputStr = tostring(input):gsub(" ", "")
if inputStr == "-" then
return "-"
elseif inputStr == "." then
return "0."
elseif inputStr == "-." then
return "-0."
end
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 > 4 then
decimalPart = decimalPart:sub(1, 4)
end
inputStr = integerPart .. (decimalPart and "." .. decimalPart or "")
end
if inputStr:sub(-1) == "." then
inputStr = inputStr:sub(1, -2)
end
if inputStr == "-" then
return "-"
elseif inputStr == "" then
return "0"
end
return inputStr
end
print(formatInput("5")) -- Output: "5"
print(formatInput("5.")) -- Output: "5"
print(formatInput("5.a")) -- Output: "5"
print(formatInput("5.6789")) -- Output: "5.6789"
print(formatInput("5.00000000")) -- Output: "5.0000"
print(formatInput("-")) -- Output: "-"
print(formatInput(".")) -- Output: "0."
print(formatInput("-.")) -- Output: "-0."
print(formatInput("5.67891")) -- Output: "5.6789"
print(formatInput("123.456789")) -- Output: "123.4567"
solution pls ty
If you want to apply it to textboxes,
local textBox = wheremytextboxis
function formatInput(input)
local inputStr = tostring(input):gsub(" ", "")
if inputStr == "-" then
return "-"
elseif inputStr == "." then
return "0."
elseif inputStr == "-." then
return "-0."
end
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 > 4 then
decimalPart = decimalPart:sub(1, 4)
end
inputStr = integerPart .. (decimalPart and "." .. decimalPart or "")
end
if inputStr:sub(-1) == "." then
inputStr = inputStr:sub(1, -2)
end
if inputStr == "-" then
return "-"
elseif inputStr == "" then
return "0"
end
return inputStr
end
textBox.FocusLost:Connect(function()
textBox.Text = formatInput(textBox.Text)
end)
Not quite. I can’t enter “5.678” for example, because it seems to erase my . after the 5. Also, I didn’t mention this, but when the textBox is blank, I’d like it to remain blank, and not turn to 0. Thanks!
Try this
function formatInput(input)
local inputStr = tostring(input):gsub(" ", "")
if inputStr == "-" then
return "-"
elseif inputStr == "." then
return "0."
elseif inputStr == "-." then
return "-0."
end
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 inputStr:sub(-1) == "." then
inputStr = inputStr:sub(1, -2)
end
if inputStr == "-" then
return "-"
elseif inputStr == "" then
return "0"
end
return inputStr
end
print(formatInput("5")) -- Output: "5"
print(formatInput("5.")) -- Output: "5"
print(formatInput("5.a")) -- Output: "5"
print(formatInput("5.6789")) -- Output: "5.679"
print(formatInput("5.00000000")) -- Output: "5.000"
print(formatInput("-")) -- Output: "-"
print(formatInput(".")) -- Output: "0."
print(formatInput("-.")) -- Output: "-0."
print(formatInput("5.67891")) -- Output: "5.678"
print(formatInput("123.456789")) -- Output: "123.456"
Almost, I just need “5.” to return “5.” since my code gets updated using TextBoxInstance:GetPropertyChangedSignal("Text")
. Thanks!
Appologies for the oversight.
Here is the new improvised version of the function.
function formatInput(input)
local inputStr = tostring(input):gsub(" ", "")
if inputStr == "-" then
return "-"
elseif inputStr == "." then
return "0."
elseif inputStr == "-." then
return "-0."
end
local hasTrailingDot = inputStr:sub(-1) == "."
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
if inputStr == "-" then
return "-"
elseif inputStr == "" then
return "0"
end
return inputStr
end
It works! Two things though: Why do you check if inputStr == "-" then
at the top and bottom? And can you explain what each section does? What in God’s name is this "^(%-?)0+(%d)", "%1%2"
!?
Thanks!
You could also do if the first character is “-” but i just did for quick reasons.
For the random characters, those are string patterns
You can read about string patterns, they extract things from strings.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.