Conversion to number
Let's say you have a string on your hands.
It looks beautiful, but you can't really count anything with it.
Is there a problem? No, it's just a moment for
tonumber(x).ode>).
The operator is clear: You feed him something, and he tries to make a number out of it. If you're lucky, you'll succeed.
And if you gave a string like "42" — no problem at all. Even "0xFF" will pick up if necessary — and immediately realize that it is 255 in hexadecimal. Comfortable.
What's under the hood? Nothing magical. tonumber takes your input and honestly tries to read it as a number. Sometimes it works. Sometimes not so much.
For example, if you passed an apple, it will return nil at best, and at worst it will throw you an exception. Then see for yourself how to deal with it.
By the way, if necessary, you can specify the number system. Do you want to extract a number from a string in binary format? Not a question. Just tell me that the base is 2.
It works for octal, hexadecimal ... in general, the imagination here is limited only to your tasks.
But make no mistake: tonumber is not a telepath. It does not count an empty string as a number. Spaces, strange characters, and chunks of text are all better handled in advance.
Otherwise, you risk getting an unexpected result... or nothing at all.
So keep in mind: tonumber is like a filter. Only those data that can be confidently calculated pass through it. The rest are passing by.
But if you build everything correctly, it will become a faithful assistant where numbers are more important than words.
Help on built-in functions
The rules of programming scripts
Programm options
Color constants