# String to Number

**URL:** <https://forum.kirupa.com/t/string-to-number/12135>\
**Category:** flash\
**Created:** [January 27, 2003, 2:39pm UTC](https://forum.kirupa.com/t/string-to-number/12135 "2003-01-27T14:39:48Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![hanzou](https://avatars.discourse-cdn.com/v4/letter/h/ed655f/32.png) [@hanzou](https://forum.kirupa.com/u/hanzou)\
**Post date:** [January 27, 2003, 2:39pm UTC](https://forum.kirupa.com/t/string-to-number/12135/1 "2003-01-27T14:39:48Z")

</div>

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?  
thanks in advance

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 27, 2003, 2:42pm UTC](https://forum.kirupa.com/t/string-to-number/12135/2 "2003-01-27T14:42:59Z")

</div>

number()

number(“16”) == 16

:beam:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 27, 2003, 6:38pm UTC](https://forum.kirupa.com/t/string-to-number/12135/3 "2003-01-27T18:38:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 27, 2003, 6:40pm UTC](https://forum.kirupa.com/t/string-to-number/12135/4 "2003-01-27T18:40:30Z")

</div>

> \*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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 27, 2003, 6:43pm UTC](https://forum.kirupa.com/t/string-to-number/12135/5 "2003-01-27T18:43:53Z")

</div>

> \*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)!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 27, 2003, 6:44pm UTC](https://forum.kirupa.com/t/string-to-number/12135/6 "2003-01-27T18:44:36Z")

</div>

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

cheers \*\*

says who?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 27, 2003, 6:56pm UTC](https://forum.kirupa.com/t/string-to-number/12135/7 "2003-01-27T18:56:42Z")

</div>

you can subtract zero from the string like:

```php
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

😉

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 27, 2003, 6:58pm UTC](https://forum.kirupa.com/t/string-to-number/12135/8 "2003-01-27T18:58:21Z")

</div>

> \*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…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 27, 2003, 7:01pm UTC](https://forum.kirupa.com/t/string-to-number/12135/9 "2003-01-27T19:01:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 28, 2003, 7:05am UTC](https://forum.kirupa.com/t/string-to-number/12135/10 "2003-01-28T07:05:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 29, 2003, 11:35am UTC](https://forum.kirupa.com/t/string-to-number/12135/11 "2003-01-29T11:35:59Z")

</div>

🙂
