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.
How to check if a value is within multiple range of numbers
The code will be something like this:
function ShowResult(numCorrect){
if(numCorrect != undefined)
{
if(numCorrect >= 0 && numCorrect < 5) {
alert("Your message for low score")
}
else if(numCorrect >= 5 && numCorrect < 8) {
alert("Your message for average score")
}
else {
alert("Your message for highscore")
}
}
}
Anyway there are better ways to handle that kind of things, have a dictionary already set up with the scores and also look for some nice tooltips, i've used toastr.js for example to have a nice impact, and you can change colors and stuff
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
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
Related Topics
Casting Plain Objects to Class Instances in JavaScript
How to Access Local Scope Dynamically in JavaScript
Why Does !New Boolean(False) Equals False in JavaScript
Tool to Unminify/Decompress JavaScript
Prevent Requirejs from Caching Required Scripts
How to Stop JavaScript Execution
How to Add Extra Info to Copied Web Text
How to Disable Text Selection Using Jquery
Should I Be Using Object Literals or Constructor Functions
Best Cross-Browser Method to Capture Ctrl+S with Jquery
Basic JavaScript Promise Implementation Attempt
JavaScript Variables Declare Outside or Inside Loop
How to Poll a Google Doc from an Add-On
Manipulating Innerhtml Removes the Event Handler of a Child Element
JavaScript Document.Write Replaces All Body Content When Using Ajax
How to Get the (X, Y) Pixel Coordinates of the Caret in Text Boxes