How to check if a number is included in a range (in one statement)?
(1..10).include?(number)
is the trick.
Btw: If you want to validate a number using ActiveModel::Validations
, you can even do:
validates_inclusion_of :number, :in => 1..10
read here about validates_inclusion_of
or the Rails 3+ way:
validates :number, :inclusion => 1..10
Check if a value is within a range of numbers
You're asking a question about numeric comparisons, so regular expressions really have nothing to do with the issue. You don't need "multiple if
" statements to do it, either:
if (x >= 0.001 && x <= 0.009) {
// something
}
You could write yourself a "between()" function:
function between(x, min, max) {
return x >= min && x <= max;
}
// ...
if (between(x, 0.001, 0.009)) {
// something
}
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&&
like1 <= 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'"
Determine whether integer is between two other integers
if 10000 <= number <= 30000:
pass
For details, see the docs.
Is there a better way to check if a number is range of two numbers
Since you have continuous, sorted ranges, a quicker and less verbose way to do this, is to use the bisect module to find the index in a list of breakpoints and then use it to get the corresponding value from a list of values:
import bisect
break_points = [5499, 9499, 14499, 19499, 24499, 29499, 34499, 39499, 44499]
values = [5000, 10000, 15000, 20000, 25000, 30000, 35000, 40000, 45000]
n = 10000
index = bisect.bisect_left(break_points, n)
values[index]
# 15000
You'll need to test for n
values that exceed the last breakpoint if that's a possibility. Alternatively you can add a default value to the end of the values list.
Can I use an if statement to check if a number is within a range?
Yes, you can! Best way is to try by yourself :)
if 3 in range(5):
print('ok')
else:
print('no')
Returns ok
How to check javascript number range?
You can't chain comparison operators like that. You have to do each comparison separately and use AND (&&) or OR (||).
var ids = 1;
if (9 <= ids && ids <= 14) {}
Excel - Way to check if number is within range in a single step?
You can use a single condition to check if the absolute value of the very long expression offset by the average value of the upper and lower bounds is less than the upper bound offset by the average value of the upper and lower bounds.
Simple example below:
0 < x < 6
(average of 0 and 6 is 3, so subtract 3 from all expressions below)
-3 < x-3 < 3
abs(x-3) < 3
To incorporate this into an Excel formula, you can do this:
= ABS({long expression}-AVERAGE({upper},{lower}))<({upper}-AVERAGE({upper},{lower}))
The only repetition that occurs in this formula above is AVERAGE({upper},{lower})
, but assuming the upper and lower bounds are either hardcoded or are single cell references, this formula will be shorter (and more efficient) than typing out the {very long expression}
twice. And actually, if the upper and lower bounds are hardcoded, you could also just hardcode the average of them to make the formula even shorter.
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
Related Topics
Suitability of Rails, Padrino, and Sinatra for Building a Prepaid Mobile Service
Don't Create View Folder on Rails Generate Controller
Run Two Different Rails Application on One Dedicated Server
Run Ruby Script in the Background
How to Dynamically Define a Class Method Which Will Refer to a Local Variable Outside
How to Dynamically Call Routes Helper in Rails
Why and When to Use the Shell Instead of Ruby
Rails Activeadmin: Showing Table of a Related Resource in the Same View
Paperclip :Style Depending on Model (Has_Many Polymorphic Images)
Runtime Changing Model with Mongodb/Mongoid
Loop Through Activerecord::Associations::Collectionproxy with Each
Convert Unicode into Character with Ruby