# how to check if a number is between one range

hello!
i know that it’s a stupid question, but i have to check continuosly if a number is between one range (negative number included) maybe is a elementary school question about math…

for example 0 < p < 1 or -1 < p< 1
thank’s you in andvance

var x = 30;
if (Enumerable.Range(1,100).Contains(x))
//true

``````    if (x >= 1 && x <= 100)
//true
``````

Thank’s everyone!
Actually i did it by building my own method, is this

``````    public bool IsBetween(double testValue, double bound1, double bound2)
{
return (testValue >= Math.Min(bound1,bound2) && testValue <= Math.Max(bound1,bound2));
}
``````

When you say “check” it just sound like you want to know when the number is inside the range or not. In this case just do this

``````if (p > minValue && p < maxValue)
{
// inside range
}
else
{
// outside range
}
``````

if you want to keep the value within the range you can use Mathf.Clamp

``````p = Mathf.Clamp(p, minValue, maxValue);
``````

public static class MathExtension
{
public static bool IsBetweenRange(this float thisValue, float value1, float value2)
{
return thisValue >= Mathf.Min(value1, value2) && thisValue <= Mathf.Max(value1, value2);
}
}