How do I turn a string such as "6" into a number?

I have a section of my code where I need to turn user input on a keyboard into a number. I know in other languages you can use built in functions, and I was wondering if Lua/Codea had any such function.

You can just do arithmetic on a string and Lua will attemp the conversion. E.g

x = 7 + "100"
-- x is 107

Or you can use tonumber("100") to convert a string to a number.

Thank you! this is so much easier than trying to make my own function to convert strings to numbers. :slight_smile:

Hello @edat44. Information about Lua’s coercion is here; information about Lua’s tonumber() function is here; and information about how Lua applies tonumber() to operands for certain operators or for statements is here and here.

So, you can code:


for i = "1", "10" do
    print(i)
end

and it ‘works’.