How to Check If an Integer Is Within a Range

How to elegantly check if a number is within a range?

There are a lot of options:

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

And indeed basic if more elegantly can be written with reversing order in the first check:

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

Also, check out this SO post for regex options.

Notes:

  • LINQ solution is strictly for style points - since Contains iterates over all items its complexity is O(range_size) and not O(1) normally expected from a range check.

    More generic version for other ranges (notice that second argument is count, not end):

    if (Enumerable.Range(start, end - start + 1).Contains(x)
  • There is temptation to write if solution without && like 1 <= x <= 100 - that look really elegant, but in C# leads to a syntax error "Operator '<=' cannot be applied to operands of type 'bool' and 'int'"

How to check if an integer is within a range of numbers in PHP?

The expression:

 ($min <= $value) && ($value <= $max)

will be true if $value is between $min and $max, inclusively

See the PHP docs for more on comparison operators

Check if an integer is within one of the ranges

You could use Array#some and check the interval.

The some() method tests whether some element in the array passes the test implemented by the provided function.

function checkAgainst(value) {

return ranges.some(function (a) {

return value >= a[0] && value <= a[1];

});

}

var ranges = [[1, 4], [6, 10], [15, 20]];



console.log(checkAgainst(3));

console.log(checkAgainst(5));

console.log(checkAgainst(300));

How to check whether an int is within a range of values

I think you need you this condition for c

(c > 1 && c < 8) // 1 and 8 are exclusive
(c => 1 && c <= 8) // 1 and 8 are inclusive

Full sample

else if (a==2 && b.equalsIgnoreCase("line")
&& (c > 1 && c < 8) && d)

If you need to check if the values belongs to a set of values, you need to use a Set and then check if the c belongs to that or not. In your original question, it was mentioned as a range and thus the answer. Anyways, this is how you can check a value in a set.

Integer[] arr = {1,4,9,11,13};
Set<Integer> set = new HashSet<>(Arrays.asList(arr));
...
else if (a==2 && b.equalsIgnoreCase("line")
&& (set.contains(c)) && d)

How to check if an integer is within a range?

Tested your 3 ways with a 1000000-times-loop.

t1_test1: ($val >= $min && $val <= $max): 0.3823 ms

t2_test2: (in_array($val, range($min, $max)): 9.3301 ms

t3_test3: (max(min($var, $max), $min) == $val): 0.7272 ms

T1 was fastest, it was basicly this:

function t1($val, $min, $max) {
return ($val >= $min && $val <= $max);
}

How to check if a number is within a range in shell

If you are using Bash, you are better off using the arithmetic expression, ((...)) for readability and flexibility:

if ((number >= 2 && number <= 5)); then
# your code
fi

To read in a loop until a valid number is entered:

#!/bin/bash

while :; do
read -p "Enter a number between 2 and 5: " number
[[ $number =~ ^[0-9]+$ ]] || { echo "Enter a valid number"; continue; }
if ((number >= 2 && number <= 5)); then
echo "valid number"
break
else
echo "number out of range, try again"
fi
done

((number >= 2 && number <= 5)) can also be written as ((2 <= number <= 5)).


See also:

  • Test whether string is a valid integer
  • How to use double or single brackets, parentheses, curly braces

Check if an integer is within a range

There are many ways of doing the same things in Ruby. You can check if value is in the range by use of following methods,

14.between?(10,20) # true

(10..20).member?(14) # true

(10..20).include?(14) # true

But, I would suggest using between? rather than member? or include?

All number literals denote inclusive ranges.
You can find more about it on Ruby in Rails.



Related Topics



Leave a reply



Submit