# String to Number

Hi, does anybody know how to convert contents of a string to numbers?
I’ve made 2 dynamic textbox with contents that vary, as long as it’s number (i use the ‘restrict’ method on the input), depends on the input from the user.
the problem is i want to compare the contents of the 2 box.
generally it works fine, but when it compares 2 digit number with 3 digits number, all is lost. for example, i want to compare 72 with 108, the result is 72 is greater than 108.
i tried to debug the movie, watching the variables, and it turns out that flash read the content not as 72 and 108, but as “72” and “108” (which is a string), so i’d have to convert them to number first before i can compare them, right?
the question is how? i’ve browsed the help topics, and the clostest i cand find is number.toString, but not the other way around.
anybody can help me?

number()

number(“16”) == 16

:beam:

a = “16”
a1 = parseInt(a)
trace(typeof a1)//returns number

If your numbers are decimals, then use parseFloat:

a = “16.66666”
a1 = parseFloat(a)
trace(typeof a1)//returns number

Working example of parseFloat:

a = “16.66666”;
a1 = parseFloat(a);
b = a1+a1;
trace(b);
//returns 33.33332

parseInt() (or float) is from ECMA script which is the standard that Macromedia has wisely switched to.

*Originally posted by senocular *
**number()

number(“16”) == 16

:beam: **

thats right, except for the n in number has to be a capital N i.e Number();

cheers

*Originally posted by ahmed *
**thats right, except for the n in number has to be a capital N i.e Number();

cheers **

It does work with having ‘n’ in lowercase! This had been discussed earlier, and there are some commands that are case-sensitive(ie: onclipevent won’t work)!

*Originally posted by ahmed *
**thats right, except for the n in number has to be a capital N i.e Number();

cheers **

says who?

you can subtract zero from the string like:

``````var speedString="100";
speedNumber=speedString-0;
trace ("type of speedNumber = "+typeof speedNumber);
trace ("value of speedNumber = "+speedNumber);
``````

the output will be:

type of speedNumber = number
value of speedNumber = 100

*Originally posted by senocular *
**says who? **

oops… well it works, but it doesnt show up highlighted in blue the way functions do when its number and not Number…

color coded Number is referencing the Number object. In using number as a conversion function, I tend not to capitalize to help make that distinction

and yes, Guig0 is right with the -, actually any non + mathmatical operation will convert the string into a number. *1 or /1 will also work

Thanx a lot, guys! i tried the ‘number’ thing, and it works perfectly. thanks for the other methods as well, i’ll keep it in mind, the next time i need it.
Thanx senocular, h88, ahmed, and guigo.