Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Can some one tell me what tostring and tonumber means?

Asked by
Tizzel40 243 Moderation Voter
5 years ago

I would like to know what these words mean

To number and tostring!

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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 
0
Thank you incapaz. So its basicly a convertable function...? Tizzel40 243 — 5y
0
`tonumber()` can also format a hex value into its decimal counterpart. print(tonumber("0x123ABC")) >> 1194684 EpicMetatableMoment 1444 — 5y
0
yeah User#19524 175 — 5y
Ad

Answer this question