# Help Width My Code

I’m trying to do to number with my code width matematic

Example:

``````print(tonumber("1 + 1")) -- Gives You Nil
``````

i ended my place!: Code.rbxl (23.4 KB)

say !Calculate and (number1) (calculation) (number2)

Could you give us more detail? Like an error in the output?

it does not give me errors, it only returns null

You are using `tonumber()`, which converts string into numbers, but that only works if you have given those strings a set number.

Here’s a guide:

You can easily convert a string to a number by using the `tonumber()` function. This function takes one argument (the string) and returns it as a number. If the string doesn’t resemble a number, for example `"Hello"` , the `tonumber()` function will return `nil` .

3 Likes

i can’t.

Yes or Yes I have to use strings because I’m doing something that only uses strings

Does it resemble a number? If the string does not resemble a number it will return nil.

so what solution is there for my case?

Oh wait you posted the article I just read. Rip.

this does not work for me I have to do this with divided strings I need strings together

if this worked then string calculators would be easier, anyways you could do

``````print(tonumber("1") + tonumber("1"))
``````

that would print out 2
anyways yea since “1 + 1” can’t be turned into a number it outputs nil

``````Local math = “1 + 1”

print(tonumber(math))
``````

This might work but I don’t know I’m not a pro scripter.

yea that wouldn’t work sorry
“1 + 1” would print out nil because you can’t turn that into a number

also Print() isn’t a thing

Oh ok. I’m not a good scripter so I would not know.

that does not work is the same

``````local var = "1 + 1"

print(tonumber(var))
``````

=

`print(tonumber("1 + 1"))`

when will they answer me???

Hang in there. I’m working on finding a solution right now.

Try this

``````local var = “1” + “1”

print(tonumber(var))``````

wait one second I already said this

are you only doing math related things?
do you want a string calculator?

I’m not very good at string manipulation. There are many ways to go about this, but this is just my simple way at achieving what you want.

``````local test = "5 + 5"

local operations = {
["+"] = function(a, b)
return a + b
end,

["-"] = function(a, b)
return a - b
end,

["*"] = function(a, b)
return a * b
end,

["/"] = function(a, b)
return a / b
end,
}

local values = string.split(test, " ")

local x = values[1]
local oper = values[2]
local y = values[3]

print(operations[oper](x, y))
``````
1 Like