tags:

views:

2452

answers:

6

Hi Not sure if this makes sense, but I need to check if a server value returned is actually a number. Right now I get ALL number values returned as strings ie '7' instead of 7.

What's the simplest way to check if string values can actually be converted to numbers?

A: 

You can notice that in actionscript :

trace(int('7')); // will return 7

and

trace(int('a')); // will return 0

So except for zeros, you can actually now if a string is a number or not

Blackethylene
A: 
typeof('7') == 'string'
typeof(7) == 'number'

Does that help?

toby
not really, since it was about determining whether a string is numeric ... btw. this is deprecated since AS3, since it provides "is" operator (AS2 had "instanceof" for that matter), property "constructor" or "flash.utils.getQualifiedClassName"
back2dos
A: 

The easiest way to do this is to actually convert the string to a Number and test to see if it's NaN. If you look at the Flex API reference, the top-level Number() function says it will return NaN if the string passed to the method cannot be converted to a Number.

Fortunately, Flex (sort of) does this for you, with the isNaN() function. All you need to do is:

var testFlag:Boolean = isNaN( someStringThatMightBeANumber );

If testFlag is false, the string can be converted to a number, otherwise it can't be converted.

Edit

The above will not work if compiling in strict mode. Instead, you will need to first convert to Number and then check for NaN, as follows:

var testFlag:Boolean = isNaN( Number( someStringThatMightBeANumber ) );
Dan
same problem as with OneNerd's answer ... will not compile ...
back2dos
OK, then we just convert to Number first. I'll make the appropriate edit.
Dan
+5  A: 

Haven't tested this, but this should work:

if( isNaN(theString) ) {
   trace("it is a string");
} else {
    trace("it is a number");
}

If you are using AS3 and/or strict mode (as pointed out by back2dos), you will need to convert to number first in order for it to compile:

if( isNaN(Number(theString)) ) {
   trace("it is a string");
} else {
    trace("it is a number");
}
OneNerd
the parameter of isNaN has to be a float ... so this will throw compiler errors in strict mode ...
back2dos
true for as3 strict mode I suppose, although that was not specified. Hardly deserves a negative vote though dude - I mean, you voted down 3 posts here. How about just putting in a comment and leaving it at that. jeesh.
OneNerd
A: 

this will try to convert your String to a Number, which essentially is a 64 bit floating point number:

var val:Number = Number(sourceString);

if sourceString is not a valid String representation of a Number, val will be NaN (not a number) ... you have check against that value with isNaN ... because val == NaN will return false for a reason that can't quite understand ... you can use int(val) == val to check, whether it is an integral value ...

back2dos
A: 

function isANumber(__str:String):Boolean { return !isNaN(Number(__str)); }