Page 1 of 1

Comparing a Number to a String

Posted: Tue Apr 26, 2016 3:44 am
by DedlySpyder
So, I have a "string" that is always a number (a page number), and sometimes when I try to reference it, everything goes smoothly, other times I get an error for comparing a string to a number.

This works:

Code: Select all

local page = selectionFrame.pageNumber.caption
if (#global.avatars > page*10) then
	updateGUI(player, page+1)
end
But this returns an error for comparing a string to a number

Code: Select all

local page = selectionFrame.pageNumber.caption
if (page > 1) then
	updateGUI(player, page-1)
end
If I try to do some math before I compare in the second example (by adding 0 or multiply by 1), then it doesn't error. BUT, the number I get is not accurate. It goes from 2 to 91, 891, 8891, etc. A qucik check for LUA says that comparing a string to a number is fine (assuming that the string is really a number). I'm at a loss.

Re: Comparing a Number to a String

Posted: Tue Apr 26, 2016 4:44 am
by steinio
I suppose a string times 10 is 0 what is smaller #global.avatars everytime.

Acoording to http://stackoverflow.com/questions/1096 ... ing-to-int there is a function tonumber which you can use.

Greetings steinio

Re: Comparing a Number to a String

Posted: Tue Apr 26, 2016 11:37 am
by DedlySpyder
Ok, so the first bit of code always worked, and I found out that the second bit would work if I did useless math (page*1) ahead of time, I was just ruining the number later on.

Your function does the same as the useless math though, and looks much nicer, so thanks for the help.