Check if value is a number
Solution 1
Simple:
if(_myValue is Number)
{
fire();
}// end if
[UPDATE]
Keep in mind that if _myValue
is of type int
or uint
, then (_myValue is Number)
will also equate to true
. If you want to know if _myValue
is a number that isn't an integer(int) or unsigned integer (uint), in other words a float, then you can simply modify the conditional as follows:
(_myValue is Number && !(_myValue is int) && !(_myValue is uint))
Let's look at an example:
package
{
import flash.display.Sprite;
import flash.events.Event;
public class Main extends Sprite
{
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
var number1:Object = 1; // int
var number2:Object = 1.1; // float
var number3:Object = 0x000000; // uint
trace(number1 is Number); // true
trace(number2 is Number); // true
trace(number3 is Number); // true
trace(number1 is Number && !(number1 is int) && !(number1 is uint)); // false
trace(number2 is Number && !(number2 is int) && !(number2 is uint)); // true
trace(number3 is Number && !(number3 is int) && !(number3 is uint)); // false
}
}
}
Solution 2
If you only want to know if myValue is one of the numeric types (Number, int, uint), you can check if (_myValue is Number)
as Taurayi suggested.
If you also want to know if _myValue is a numeric string (like "6320" or "5.987"), use this:
if (!isNaN(Number(_myValue)))
{
fire();
}
It uses Number(_myValue)
to cast _myValue
to the Number
class. If Number
is unable to convert it into a useful number it will return NaN
, so we use !isNaN()
to make sure the returned value is not "not a number".
It will return true for any variable of type Number
(as long as its value isn't NaN
), int
, uint
, and strings that contain a valid representation of a number.
Solution 3
These methods could be problematic if you wish to check the input of a text field, which is 'always' a string. If you have a string with "123" and check with "123" is Number, you will get a false. So Number("123") would give true, but then again so will Number("lalala") (event though the result is NaN which will tell you NaN is Number (true).
To work with string you could do:
var s:String = "1234";
String(Number(s)) == String(s);
--True
var s:String = "lalala";
String(Number(s)) == String(s);
--False
Solution 4
There are
Related videos on Youtube
Comments
-
mate64 about 4 years
How can i simply check if a returned value of type
int
oruint
is a number? -
Triynko over 11 years"_myValue is Number" will still be true and typeof(_myValue) will still be "number" even if _myValue is typed as int or uint. According to stackoverflow.com/a/9447869/88409 Flash stores integer values as ints, and as Number only if there is a fractional part in the value or it exceeds 0x0FFFFFFF (highest value that can be stored in the remaining 28-bits of the 32-bit atom that reserves 3 bits for a type description and 1 bit for the sign). See also stackoverflow.com/a/2697151/88409 and a test case here: troyworks.com/blog/2007/12/02/as3-understanding-uint-int-number
-
Robert over 11 yearsQuestioner wants to know if _myValue is a number, not a Number.
-
Taurayi over 11 years@Robert sorry I can be a bit of a dunce sometimes, not quite sure what you are getting at.
-
Robert over 11 years@Taurayi Your method returns false for valid numeric strings. The question is ambiguous but I assumed he was looking for a way to tell if something is a number, not just if it's of a numeric type.
-
Triynko about 10 yearsIMO, this is the best answer. It's exactly what I would have posted. Although it doesn't completely explain the relationship between Number, int, and uint (i.e. any int is a Number, but Numbers aren't necessarily ints or uints), this implementation is better than the conditional checks, in part because it also takes numeric strings into account.
-
Triynko about 10 yearsTo account for numeric strings as well as Number, int, and uint, the test is much simpler:
var isNumeric:Boolean = !isNaN(Number(value));
. That will handle any Number value, but will also try to convert strings to numbers. If it's a number or can be converted to a number, then it's a numeric value. No need to test against Number, int, and uint types with theis
operator explicitly. -
Robert about 10 years@Triynko I edited my answer to better explain about the numeric types.