I use Number.toString
method to convert numbers to string.
Number(100).toString(16);
Number(1000).toString(36);
How can I convert them back to the original numbers?
I use Number.toString
method to convert numbers to string.
Number(100).toString(16);
Number(1000).toString(36);
How can I convert them back to the original numbers?
By using parseInt
.
parseInt(Number(100).toString(16), 16);
parseInt(Number(1000).toString(36), 36);
Number("10") === 10
You can pass any string into Number and it will turn it into a number object. I prefer this method since if it contains any non-number characters, it will resolve to NaN
which is what you want most of the time.
If you're parsing a string which begins with a number but may contain alpha characters afterwards, parseInt
and parseFloat
both will parse the leading numerics out - but you can forget about any numbers which may be mixed into the string elsewhere.