Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I calculate a stringed math problem?

Asked by
Drodex 2
4 years ago
calculation = "1+1"

print(calculation)

--output: 1+1

How do I unstringify this in Roblox Lua? I don't want to use loadstring due to security issues nor does math.eval work...

What are my options to make this work?

I've tried to format it like this but it doesn't work, it outputs nil:

--I used string.byte to get the values of 1 and + and then I did this:

print(tonumber(string.char(49)) .. string.char(43)) .. tonumber(string.char(49)))

--I want to calculate it in a print but it doesn't work.

The characters + and -, are they strings or numbers???

0
Use :sub() OnaKat 444 — 4y

2 answers

Log in to vote
0
Answered by
EteraW 77
4 years ago

I have made a new script to calculate it.

local calculate = "3+31"
print(calculate)
print()

local num1 = nil
local numA = nil
local symbol = nil
local num2 = nil
for i = 1,100,1 do
    local sub = calculate:sub(1,i)
    if tonumber(sub) ~= nil then
        num1 = sub
        numA = i
    end
end
symbol = calculate:sub(numA+1,numA+1)
for i = numA+2,100,1 do
    local sub = calculate:sub(numA+2,i)
    if tonumber(sub) ~= nil then
        num2 = sub
    end
end
if symbol == "+" then
    print(num1 + num2)
elseif symbol == "-" then
    print(num1 - num2)
elseif symbol == "*" then
    print(num1 * num2)
elseif symbol == "/" then
    print(num1 / num2)
elseif symbol == "^" then
    print(num1 ^ num2)
elseif symbol == "%" then
    print(num1 % num2)
end
0
Thanks for this, I actually had something similar done but my version didn't work. Drodex 2 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

idk what unstring mean so i just be dumb and put this

print(1 + 1)

 -- output: 2

Answer this question