|
| 13 Jan 2013 05:22 PM |
The "getNumberType" ALWAYS returns "not a number" no matter is it or not, please help?
Also, this is not roblox Lua, this is standard Lua 5.1.
function getNumberType(n) local result if type(n) == "number" then local str = tostring(n) if string.sub(str, 1, 2) == "0." then result = "Decimal" else result = "Integer" end else result = "Not a number" end return result end
function wait(t) local old = os.time() repeat until os.time() == old + t end
while true do clear() io.write("Type a number: ") local num = io.read() print(getNumberType(num)) wait(5) end |
|
|
| Report Abuse |
|
|
|
| 13 Jan 2013 05:43 PM |
The type of a string which looks like a number is a string:
print(type("23")) -->string
So, you need
if type(n) == "string" and tonumber(n) then n = tonumber(n) end
at the beginning of the function. |
|
|
| Report Abuse |
|
|
|
| 14 Jan 2013 02:46 AM |
I tried doing it in a new way doing also what you said, now any number always returns decimal..
function getNumberType(n) local result if type(n) == "string" and tonumber(n) then n = tostring(n) if string.find(n, ".") then result = "Decimal" else result = "Integer" end else result = "Not a number" end return result end
function wait(t) local old = os.time() repeat until os.time() == old + t end
while true do clear() io.write("Type a number: ") local num = io.read() print(getNumberType(num)) wait(5) end |
|
|
| Report Abuse |
|
|
| |
|
| |
|
1Topcop
|
  |
| Joined: 09 Jun 2009 |
| Total Posts: 6635 |
|
|
| 14 Jan 2013 11:18 AM |
function nType(n) if(tonumber(n))then if(tostring(n):match("."))then return "Double/Float" else return "Integer"end else return "Not a number." end end
print(nType("This SHOULD work")) print(nType(16)) print(nType(10.12))
> Not a number. > Integer > Double/Float |
|
|
| Report Abuse |
|
|
|
| 14 Jan 2013 11:33 AM |
function getNumberType(n) local result if type(n) == "string" and tonumber(n) then n = tostring(n) if string.find(n, "%.") then result = "Decimal" else result = "Integer" end else result = "Not a number" end return result end
function wait(t) local old = os.time() repeat until os.time() == old + t end
while true do clear() io.write("Type a number: ") local num = io.read() print(getNumberType(num)) wait(5) end
Maybe it was mistaking your search for a string pattern (where '.' is any character), so I made that '%.' (a decimal point)
Probably won't work >_> |
|
|
| Report Abuse |
|
|
|
| 14 Jan 2013 05:35 PM |
| Thanks woodstauk, it did work! :D |
|
|
| Report Abuse |
|
|