I would like to know what these words mean
To number and tostring!
The tostring
function, as its name implies, returns its argument as a string. Take this for example:
local numberTwoAsString = tostring(2) print(numberTwoAsString == 2) -- false, because a number cannot equal a string print(numberTwoAsString == "2") -- true, because we converted 2 into a string
The tonumber
function, similar to how tostring
converts its argument to a string, tonumber
will instead attempt to convert its argument to a number. Heavy emphasis on attempt. tonumber
will return nil
if its argument cannot be converted to a string. This is why it is important to be careful with tonumber
, especially if you are going to perform arithmetic on something you aren't sure of being a number.
local stringFiveAsNumber = tonumber("5") print(stringFiveAsNumber == "5") -- false, we converted 5 to a number local thisWillBeNil = tonumber("abcdef") print(thisWillBeNil) --> nil