Hi. So I made a calculator gui but there is an issue that I’m currently having. Whenever I input the second value for an operation as a decimal, the calculator returns an error message for the output.
The issue only occurs when the first two characters are 0.
but it works completely fine when I input any other digit.
Video of the issue:
Edit: forgot to post video earlier. (See second post from me)
Local script:
local calculator_gui = script.Parent.Parent
local calculator = calculator_gui.Frame
local screen = calculator.ScreenFrame.Screen
local operationScreen = calculator.ScreenFrame.Operation
local keypad = calculator.Keypad
local operationPad = keypad.Operations
local numberPad = keypad.NumberKeys
local currentOperation = nil
local previousOperation = nil
local previousNumber = nil
local currentNumber = nil
local lastKeyPressed = nil
local canUse = true
local err = false
local answerGiven = false
local operationInProgress = false
local numberKeys = {}
local operations = {
["add"] = {
str = "+";
func = function(a, b)
return a + b
end
};
["subtract"] = {
str = "-";
func = function(a, b)
return a - b
end
};
["multiply"] = {
str = "x";
func = function(a, b)
return a * b
end
};
["divide"] = {
str = "/";
func = function(a, b)
return a/b
end
};
}
local otherKeys = {}
local operationKeys = {}
local output = {tostring(0)}
local err_msg = "Error. Press 'AC' key to clear."
for _, key in pairs(numberPad:GetChildren()) do
if key:IsA("TextButton") and key.LayoutOrder <= 10 then
table.insert(numberKeys, key)
end
end
for _, key in pairs(numberPad:GetChildren()) do
if key:IsA("TextButton") and key.LayoutOrder > 10 then
table.insert(otherKeys, key)
end
end
for _, key in pairs(operationPad:GetChildren()) do
if key:IsA("TextButton") and (key.Name ~= "Clear") and (key.Name ~= "Answer") then
table.insert(operationKeys, key)
end
end
for _, key in pairs(numberKeys) do
if key.LayoutOrder > 9 then
key.Text = 0
else
key.Text = key.LayoutOrder
end
end
table.sort(numberKeys, function(a, b)
return a.LayoutOrder < b.LayoutOrder
end)
local function evaluate(a, b)
local value
if currentOperation == "+" then
value = operations.add.func(a, b)
elseif currentOperation == "-" then
value = operations.subtract.func(a, b)
elseif currentOperation == "x" then
value = operations.multiply.func(a, b)
elseif currentOperation == "/" then
value = operations.divide.func(a, b)
end
return value
end
local function roundNumber(value)
return math.round(value*1e10)/1e10
end
local function showOperation()
if currentOperation ~= nil then
return currentOperation
else
return ""
end
end
local function performOperation(key)
if canUse and not err then
if table.find(numberKeys, key) then
if lastKeyPressed == tostring(currentOperation) then
currentNumber = nil
previousNumber = tonumber(table.concat(output, ""))
table.clear(output)
end
if answerGiven then
answerGiven = false
previousNumber = nil
currentOperation = nil
table.clear(output)
end
if (output[1] == "0") then
table.remove(output, 1)
end
if key.LayoutOrder > 9 then
table.insert(output, tostring(0))
lastKeyPressed = 0
else
table.insert(output, tostring(key.LayoutOrder))
lastKeyPressed = key.LayoutOrder
end
print(key.LayoutOrder)
end
if table.find(otherKeys, key) then
if key.Name == "DecimalPoint" then
if table.find(output, key.Text) == nil then
if answerGiven or (lastKeyPressed == currentOperation) then
answerGiven = false
table.clear(output)
table.insert(output, tostring(0)..key.Text)
else
table.insert(output, key.Text)
end
end
end
lastKeyPressed = "."
end
if table.find(operationKeys, key) then
previousOperation = currentOperation
local num = tonumber(table.concat(output, ""))
if (num ~= nil) and (previousNumber ~= nil) then
table.clear(output)
local answer = evaluate(previousNumber, num)
if tostring(answer):len() > 12 then
answer = roundNumber(answer)
end
table.insert(output, answer)
end
currentOperation = operations[key.Name].str
lastKeyPressed = currentOperation
operationInProgress = true
answerGiven = false
end
if key.Name == "Answer" then
if (previousNumber ~= nil) and (currentOperation ~= nil) and (currentNumber == nil) then
currentNumber = tonumber(table.concat(output, ""))
local answer = evaluate(previousNumber, currentNumber)
table.clear(output)
if tostring(answer):len() > 12 then
answer = roundNumber(answer)
end
if answer == math.huge then
err = true
canUse = false
table.clear(output)
answer = err_msg
end
table.insert(output, answer)
previousNumber = nil
elseif (currentNumber == nil) and (currentOperation == nil) then
local answer = tonumber(table.concat(output, ""))
table.clear(output)
table.insert(output, answer)
elseif answerGiven == true then
return
else
err = true
canUse = false
table.clear(output)
table.insert(output, err_msg)
end
lastKeyPressed = "="
answerGiven = true
operationInProgress = false
end
end
if key.Name == "Clear" then
table.clear(output)
currentOperation = nil
previousOperation = nil
currentNumber = nil
previousNumber = nil
lastKeyPressed = nil
answerGiven = false
operationInProgress = false
err = false
canUse = true
table.insert(output, tostring(0))
end
print(lastKeyPressed)
print(table.concat(output, ""))
end
for _, key in pairs(keypad:GetDescendants()) do
if key:IsA("TextButton") then
key.MouseButton1Click:Connect(function()
performOperation(key)
end)
end
end
while wait() do
screen.Text = table.concat(output, "")
end
Thank you for reading. Any help or suggestions are appreciated!
Edit: the issue only occurs when I input the decimal without inputting zero first (The zero is automatically placed.)