Calculator using strings

So explanation is
I have a string it says
10/10/10+10
ok?
now normally using loadstring the answer would be 10.1 but as loadstring is dangerous I want to use string.split and gsub and sub and idk what more to do that
I want a system to sperate the first 3 tens leaving the last one with the + so
10/10/10+10
after fraction
0.1+10
after addition
10.1

How would I achieve such wizardry?

pls help me D;

Won’t that do that automatically for you?

10/10/10 = 0.1,
0.1 + 10 = 10.1

If it’s a string, can’t you just use patterns [%d] to switch them into numbers and get the result?
[or maybe using tonumber could help]

nope that wont do automatically that would if i used loadstring which i dont want to cause exploiters and stuff

why dont just use:

local stringg = 10/10/10 + 10

local tostringg = tostring(stringg)

So, I’d say to get the right area of the 10’s string by doing

local fraction = string:sub("10/10/10+10", 1, 9) -- take note, an empty space `""` counts as a character, so you're always starting with 1

then afterwards, now what you can do is separate that fraction string by getting “/”, then afterwards making a math function manually:

local tens = fraction:split("/") -- This will make a table of all of the values that was "splitted".

local theNewFraction = tonumber(tens[1]) / tonumber(tens[2]) / tonumber(tens[3]) + tonumber(string:split("10/10/10+10", 9)) -- This is just using that table and making it into an actual math function.

Using it in an actual code, it should look something like this:

local fraction = string:sub("10/10/10+10", 1, 9)
local tens = fraction:split("/")
local theNewFraction = tonumber(tens[1]) / tonumber(tens[2]) / tonumber(tens[3]) + tonumber(string:split("10/10/10+10", 9))

print(tostring(theNewFraction))
1 Like

A very complicated way of doing the math with a string, but it should work. Let me know what type of problems you encounter using this.

so @edozune and @Super_pro322222 i am sorry to say but both of these methods are not what I want you see I am making a calculator! and it shouldnt use loadstring cause its bad backdoors and stuff anyways the equation is not always 10/10/10+10 it can be different most of the times

so if you’re making a calculator then just replace 10/10/10 with input1/input2/input3 + input4

inputs should be tonumber(string)

i think you still dont get it
so another example

lets say a player inputted 10+10-10
now if i do string.split(“10+10-10”,"+") it would return {“10 10-10”} see thats the problem
roblox would just error unless… wait a second HOLD UP

This isn’t something that can be done easily. You will need to learn to write a parser, and it’s not something that can be done in an afternoon. Do a search for arithmetic parser.

yea i know its complex as hell but rn i am trying to make a simpler one using tables ill make sure to keep all of you updated!

well I made this and it sucks at math more than a 3rd grader

local subtab = {}
local addtab = {}
local function solve(input)
	local sub = string.split(input,"-")
	for i,v in pairs(sub) do
		if not string.find(v,"+") then
			table.insert(subtab,v)
		else
			local add = string.split(v,"+")
			for _,a in pairs(add) do
				table.insert(addtab,a)
			end
		end
	end
	
	local num = 0
	for _,a in pairs(addtab) do
		num += a
	end
	for _,s in pairs(subtab) do
		num -= s
	end
	print(num)
end

solve("5-10-10")

well it works now

local r = loadstring("5-10-10")
r()

:+1:
yup it surely does

What if you’ve actually gotten the numbers and operations as strings?

Like, make a table for all possible number and operation values:

numbers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
operations = {"+", "-", "*", "/", "^", "%", "sqr", "log"}

Then make it so that it’ll find if the string has any of that in there, it’ll find the first and add it onto an expressed equation. Just an idea.

that is a fine idea and would try that in the future but rn ama stick to da 2 lines of code that makes everything work : ) (yea i gave up)

I’ll try to do this on my own research, would it be okay to PM you when I found a solution? Maybe you can use it for your calculator.

yea sure i dont see why not! and its not like necessary for you to do search you know?

Yeah, but you wanted to do it without using a loadstring and I know it’s possible. So, I just wanted to kind of fulfill the initial request, which was doing it without the loadstring.

1 Like

hmm that is right and thank you very much!

1 Like

@Qinrir, JUST FOUND A WAY TO DO IT WITHOUT LOADSTRING.

So, using a module called: Luaxp, it can help with making a string a mathmatical function, and it’ll automatically return as a string back.

Put the module in ReplicatedStorage and put this code inside of your script, modify it to add more conditions and valla!

local parser = require(game.ReplicatedStorage:WaitForChild("MainModule"))
local expression

TextBox = script.Parent.TextBox

if TextBox then


	for buttons, button in ipairs(script.Parent:GetChildren()) do

		if button:IsA("TextButton") then

			if button.Name == "=" then
				
				button.MouseButton1Click:Connect(function()
					
				
					expression = parser.evaluate(TextBox.Text)
					wait()
					TextBox.Text = expression
				end)
				
			elseif button.Name == "C" then
				
				button.MouseButton1Click:Connect(function()
					
					TextBox.Text = ""
				end)
				
			elseif button.Name == "CE" then
				
				button.MouseButton1Click:Connect(function()
					
					TextBox.Text = TextBox.Text:gsub(".?$","")
				end)
				
				-- Going to assume you have more buttons like ^, sqr, log, etc. Like how the if statements are set up, make the button name be the symbol, and then put the symbol in as a text.
			else
				
				button.MouseButton1Click:Connect(function()
					
					TextBox.Text ..= button.Text
				end)
			end
		end
	end
end
2 Likes