hello friends ;
I am new a little bit in corona sdk and i faced issue in my code.
i wrote input ntaive text and i wont to comapre it with exact value (for example 10) , if the user write 10 will print (“the numbers are equal”) elseif print ("the numbers are not equal ")
but always is printed (“the numbers are not equal”) ?!!
this is some part of my code:
uInput= native.newTextField(155,180,180,30)
uInput.inputType = “number”
uInput:addEventListener(“userinput”,textLesitener)
UI=uInput.text
print (UI)
local function checkUser( )
if 10== UI then print ("the numbers are equal ")
else print ("the numbers are not equal ")
end
end
i hope to solve my problem
thanks
Hi @haro0on.77,
This is because whatever you type into the native text input (even if you limit it to “number” input type) comes back as a string , and Lua numbers don’t exactly equal Lua strings.
To compare a string which is numbers to an actual number, use the “tonumber” conversion like this:
[lua]
if 10 == tonumber(UI) then print ("the numbers are equal ")
[/lua]
Hope this helps,
Brent
as you said , now it work fine
thank you Brent
Hi @haro0on.77,
This is because whatever you type into the native text input (even if you limit it to “number” input type) comes back as a string , and Lua numbers don’t exactly equal Lua strings.
To compare a string which is numbers to an actual number, use the “tonumber” conversion like this:
[lua]
if 10 == tonumber(UI) then print ("the numbers are equal ")
[/lua]
Hope this helps,
Brent
as you said , now it work fine
thank you Brent