How to check if a number is between two other numbers in Actionscript 3?

5.8k Views Asked by At

How can i check if a number is between two other numbers like:

pseudocode:

var = 458;

if (var is between 0 and 1000) give positive.

if (var is between 1001 and 2000) give negative.

if (var is between 2001 and 3000) give negative.

in AS3?

Thanks in advance.

3

There are 3 best solutions below

1
On BEST ANSWER

If You will check it many times, simply create function :

function check(min:Number , value:Number , max:Number):Boolean{
    return min > value ? false : ( max < value ? false : true );
}

It will return true if value is between min and max.

0
On
if (var >= 0 && var <= 1000) {
  return true
}
else if (var >= 1001 && var <= 2000) {
  return false
}
else if (var >= 2001 && var <= 3000) {
  return false
}

But conditions 2 and 3 both return false, and the condition also evaluate to true/false, so you could simply:

return (var >= 0 && var <= 1000)
0
On

There's a method in the framework just for that:

mx.utils.ObjectUtil::numericComapre()

From the docs:

Compares two numeric values. Returns int — 0 is both numbers are NaN. 1 if only a is a NaN. -1 if only b is a NaN. -1 if a is less than b. 1 if a is greater than b.