tonumber

Parses a number contained in a string. Useful for retrieving numeric values from user input. If str cannot be parsed as a number, the function will return nil.

ExampleAdd 5 to a number entered by the user
Prompt the user for a number, then increment it by 5 and print the result.
Code
<nowiki>
local num = tonumber(read())
if num == nil then
    printError("Please enter a number!")
else
    print(num + 5)
end
    </nowiki>
Output If the user did not enter a valid number, the program will print "Please enter a number!". If the user did enter a valid number, it will print its value + 5.

tonumber
Function
Syntax
tonumber(
  • str : string
)

Returns number | nil
API Base globals
Source Lua (source)
This article is issued from Computercraft. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.