This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Xoandbit's profile picture

Number detect

Started by Xoandbit, 04 March 2012 - 11:33 AM
Xoandbit #1
Posted 04 March 2012 - 12:33 PM
How i can detect if input is number?

This code dont work… It's returns false :unsure:/>/> http://pastebin.com/hyB8peMK

I am working on program - Mailman..
And in next version i need input validation… B)/>/>
MysticT #2
Posted 04 March 2012 - 01:48 PM
If you want to know if it's a number (and not the word "number"), you have to use tonumber, it returns the number if it can convert it from the string, or nil otherwise.
Example:

local input = read()
local num = tonumber(input)
if num ~= nil then
  print(input.." is a number")
else
  print(input.." is not a number")
end
Espen #3
Posted 04 March 2012 - 02:05 PM
You can also make use of:
type(variable)
It returns "string" for a string, "number" for a number, "table" for a table, etc.
Xoandbit #4
Posted 04 March 2012 - 07:43 PM
Ok… I resolved my problem … I missed type(variable)=="number" … fail ..

Thank.. :unsure:/>/>