0

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?

Lewis
  • 14,132
  • 12
  • 66
  • 87

2 Answers2

2

By using parseInt.

parseInt(Number(100).toString(16), 16);
parseInt(Number(1000).toString(36), 36);
Oka
  • 23,367
  • 6
  • 42
  • 53
  • 2
    I feel really stupid since I've already tried `parseInt` with radix `10`. Thanks for your answer. – Lewis Jun 21 '15 at 04:19
0
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.

papiro
  • 2,158
  • 1
  • 20
  • 29