0

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.

Carlos Barbosa
  • 1,422
  • 2
  • 23
  • 42

3 Answers3

6

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.

turbosqel
  • 1,542
  • 11
  • 20
  • 1
    +1 I would go with this answer, however you may want to switch the position of the `min` and `value` parameters so it becomes `function check(value:Number, min:Number, max:Number):Boolean`. – Taurayi Oct 23 '11 at 14:23
2
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)
ariel
  • 15,620
  • 12
  • 61
  • 73
1

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.