I would like to know what these words mean
To number and tostring!
I would like to know what these words mean
To number and tostring!
#tostring
The tostringfunction, 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
#tonumber
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
This discussion is preserved as it was. New answers, comments, and votes are closed.
3 comments
Thank you incapaz. So its basicly a convertable function...?
tonumber()can also format a hex value into its decimal counterpart. print(tonumber("0x123ABC")) >> 1194684yeah