PDA

View Full Version : string to number


alaios
08-14-2002, 09:40 PM
Is there any way to convert a number to a string ? And a string to a number?

joh6nn
08-14-2002, 09:59 PM
if you have a number, then you can convert it to a string in any of the following ways:

numberVar.toString();
numberVar += "";
String(numberVar);

the advantage of using the toString() method, is that you can specify the radix. eg, numberVar.toString(16) will convert numberVar to it's base-16 (hexadecimal) equivalent.

you can convert a string to a number in either of the following ways.

Number(stringVar);
stringVar -= 0;

jkd
08-14-2002, 10:56 PM
You also have the parseInt() and parseFloat() methods available for turning strings in numbers.

parseInt('5.0') == 5

parseFloat('5.27') == 5.27

parseInt also takes a second argument:

parseInt('ff', 16) == 255

which specifies the base of the number. :)