Jump to content

What's the different between tonumber and tostring


Hero192

Recommended Posts

Posted

Hey guys, i want to be sure of these lua functions ,

tonumber  and  tostring 

don't recomand me the lua.org website because it's hard to get it from them.

Give me some examples only if you are sure you know about these functions i wont get something wrong

Thanks :D

Posted

tonumber and tostring are pretty self-explanatory. tonumber converts i.e strings to number and tostring converts to string.

Posted

For example:

i have a table i will get something from the table and convert them and tell me if im wrong or right

local Table= {  
{"Jack",900}, 
} 
for index, v in ipairs (Table) do 
outputChatBox(tostring(v[1]).. " got " ..tonumber(v[2]).."$",root,255,0,0) 
end 

Posted

Clearly wrong. The values in the table are already of the type you're converting to. You use these functions when you have, for example, a variable of type number, and want to convert it to a string type, or vice versa.

Posted

You'd use tonumber() if you have a string that you want to convert to a number:

900 == tonumber("900") 
  
-- or 
  
local variable = "1275" 
-- If here you do: 
variable + 50 
-- it will return an error because 'variable' is a string. You'd need to do: 
tonumber(variable) + 50 
  

It's the same otherwise.

  
"900" ~= 900 
  
It'd need to be 
  
"900" == tostring(900) 
  
 

Hope that helps...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...