Updated May 15, 2023
Introduction to Lua tonumber
The Lua tonumber is one of the basic function for the lua script and it is mainly used for to convert the arguments to the number format. The argument will be already either string or number format by using the tonumber method. It converts to the number type and it always return the number otherwise it will return the nil value the optional argument will specifies the base to compile and interpret. The numerals the base value may be the either integer or number types that between the some number ranges if the argument is in string types it accepts both upper and lower case characters apart from that the base values is accept only unsigned integer numbers.
Syntax:
In Lua script we used lot of default methods, keywords and functions for to implement the application as per the requirements. Like that tonumber() is one of the built-in function for to convert the other types to number formats.
type(local) variable = values
type var = tonumber(variable)
-----some lua script code logics depends upon the user requirement-----
The above codes are the basic syntax for to convert the one type of values to another type like number format using tonumber() method we can converted the values to number or integer formats.
How tonumber function works in Lua?
The tonumber() method is used for to convert the arguments to the number format argument may be any type like strings etc. If suppose the argument or parameters will be already the number or the string formats if the value is string format means it will be converted to the number format using tonumber() method. Or else if the input type is already number type if also use tonumber() method is required on the script means it will return the same number or else it return the nil value. Also the optional arguments or not-required arguments specifies the base value to interpret the numeral characters. Also the string type input values may be supported for either uppercase or lowercase characters if the base value is above 10 the number format is automatically changed to characters that is upper case letter like “A” and 11 is “B” like hexadecimal format. But here A to Z is used and denote with the number like A=10 so forth it continues and randomly assign the numbers to upper case letter like Z=35. But always the base is 10 it’s the default value for the decimal numbers so as well as an optional exponent part but in other bases are supported for only the unsigned integer numbers.
Example #1
Code:
local stringnumber = "10"
local a = tonumber(stringnumber)
print(a + 10)
local a = "10"
print(type(a))
local num = tonumber(a)
print(type(num))
i = 1
while (i <= 20) do
io.write(i)
i = i + 1
if i == 13 then break end
end
print("\n Please continue your inputs")
repeat
io.write("Welcome To My Domain its a first example we have used the tonumber() method for to convert the string or any other types to number formats:")
rslt = io.read()
until tonumber(rslt) == 17
for i = 1, 20, 3 do
io.write(i)
end
print("Your inputs are validated and continue with our next step process")
mnths = {"January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November",
"December"}
for k, v in pairs(mnths) do
io.write(v, " Your input values are iterated and its validated using the key, value pair process ")
end
print("Your first example is over")
Output:
In above example, we used tonumber() method in basic logic like we have declare the variable stringnumber with local type and assign the values like numbers with double-quotes. So the input value will take it as string type by using the tonumber() method we can convert the string to number type and print it on the output console.
Example #2
local ip = ‘1234.957 welcome please changed your password in your system and everywhere used and to incorrect. Else That way we can forget it to always reminds you, Your password is incorrect and please enter correct password. or Please use forget password option’
Code:
local num = tonumber(ip)
print(num)
eg1=0
eg2 = io.read()
eg3 = io.read()
reslt = {}
for i=1,tonumber(eg2) do
reslt[i]=io.read()
end
eg4 = {}
for i=1,tonumber(eg2)*2+1 do
eg4[i]=0
end
for i=1,tonumber(eg2) do
eg4[tonumber(eg2)+1+tonumber(reslt[i])]=eg4[tonumber(eg2)+1+tonumber(reslt[i])]+1
end
eg5 = {}
l = 1
for i=1, 3*tonumber(eg2)+1 do
if eg4[i] >= tonumber(k) then
eg5[l]=i-tonumber(eg2)-1
l=l+1
end
end
io.write(l-1,"Your input numbers are iterated")
for i=1,l-1 do
io.write(tostring(eg5[i])," ")
end
print("Your second examples are completed and thanks for calculating the string value to numbers")
local rt=tonumber(ip)
print(rt)
Output:
In second example we have assigned the variable ip with local type have the values like both number and string types. But when we use tonumber() method it returns only the nil as the output because the input contains string characters so that it returns nil value.
Example #3
Code:
local vars
print("Welcome To the Third example")
local result=tonumber(vars)
print("Your input is converted to number and please find the output\n",result)
function demo(vars1,vars)
if vars<vars1
then
vars = vars+1
return vars, vars*vars
end
print("This method is used for to calculate the square of the given numbers while we iterate the values using the below for loop")
end
for i,num in demo,6,0
do
print(i,num)
end
print("Your third example is over")
Output:
In final example, we used to calculate the square of the numbers. By using the for loop we can iterate the values and squared the numbers. And also in first line we have declare the local type with the variable like vars we have not mentioned any values and also we have declared another variable result by using the tonumber() method we can convert the vars type value to number type that is casting is performed but here we have not assigned any input values so that it returns nil has the output.
Conclusion
The tonumber() method from the Lua script will generally used it on the casting purpose that is whatever the user assigned the input values with another type like string, arrays, etc. It may be converted to the number with the help of tonumber() method. Like that Lua script uses many number of casting methods among that tonumber is one of the frequent method which help for converting the data to number format.
Recommended Articles
We hope that this EDUCBA information on “Lua tonumber” was beneficial to you. You can view EDUCBA’s recommended articles for more information.