Forum Moderators: open

Message Too Old, No Replies

atoi

Is there an equivelent in javascript to C/C++'s atoi function?

         

Greven

1:28 am on May 24, 2005 (gmt 0)

10+ Year Member



Basically, I have a string that I need to parse through to find the number. So, for example:

How many inches are in 4 feet?

I need to go word by word and check which one is the number. I know in C/C++ I could check the return value of atoi to see if its -1, but I can't find an equivelent.

I've tried checking the results of Number("asdf") for example, which returns NaN in an alert, but even

alert(Number("a") == Number("a"))

returns false. Any ideas?

Glacai

11:07 am on May 24, 2005 (gmt 0)

10+ Year Member



Hi Greven, how about parseInt()?

AWildman

12:11 pm on May 24, 2005 (gmt 0)

10+ Year Member



What if you break down the sentence into each char and then use a regular expression to test whether each char is a digit?

var sentence = "How many inches are in 4 feet?";
var sentenceArray = sentence.split("");
var req = /\d/;
for(i = 0; i < sentenceArray.length; i++)
{
var test_sentence = req.test(sentenceArray[i]);
}