Check if value is a number
Asked Answered
H

4

10

How can i simply check if a returned value of type int or uint is a number?

Herson answered 17/2, 2011 at 8:53 Comment(0)
B
13

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

        }

    }

}
Baroness answered 17/2, 2011 at 9:4 Comment(5)
"_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 https://mcmap.net/q/911626/-in-actionscript-3-why-is-the-dynamic-type-of-large-int-values-number 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 https://mcmap.net/q/1161568/-in-actionscript-how-to-tell-whether-the-type-of-a-number-if-number-or-int-or-uint and a test case here: troyworks.com/blog/2007/12/02/as3-understanding-uint-int-numberVentre
Questioner wants to know if _myValue is a number, not a Number.Ritualism
@Ritualism sorry I can be a bit of a dunce sometimes, not quite sure what you are getting at.Baroness
@Baroness 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.Ritualism
To 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 the is operator explicitly.Ventre
R
5

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.

Ritualism answered 15/11, 2012 at 22:28 Comment(2)
IMO, 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.Ventre
@Ventre I edited my answer to better explain about the numeric types.Ritualism
J
4

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
Jotham answered 24/8, 2012 at 10:2 Comment(0)
S
3

There are

  • isNaN (You will want to negate this)
  • typeof (Not sure how strongly type Number works)
  • and is (which was already mentioned, again I am not sure how strong, types hold)
Slurry answered 17/2, 2011 at 9:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.