Variables to numbers

Posted by Rangon on Sat 08 Apr 2006 02:05 PM — 2 posts, 12,182 views.

#0
I'm having a problem locating a way to turn a stored string variable into a number so that math can be done on it. I'm currently programming all of my information in lua. Any ideas?
Australia Forum Administrator #1
For a start, you don't really have to, as Lua converts strings to numbers internally where the context requires. eg.


print ("2" + "3") --> 5


However if you want to do it explicitly, you can use "tonumber", like this:


a = "42"
b = tonumber (a)
print (b, type (b)) --> 42 number


This is documented on this page:

http://www.gammon.com.au/scripts/doc.php?general=lua_base