Javascript 检查值是否在数字范围内
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6454198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if a value is within a range of numbers
提问by Sotiris
I want to check if a value is in an accepted range. If yes, to do something; otherwise, something else.
我想检查一个值是否在可接受的范围内。如果是,做某事;否则,别的东西。
The range is 0.001-0.009
. I know how to use multiple if
to check this, but I want to know if there is any way to check it in a single if
statement.
范围是0.001-0.009
。我知道如何使用 multipleif
来检查这个,但我想知道是否有任何方法可以在单个if
语句中检查它。
回答by Pointy
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
”语句来执行此操作:
if (x >= 0.001 && x <= 0.009) {
// something
}
You could write yourself a "between()" function:
你可以自己写一个“between()”函数:
function between(x, min, max) {
return x >= min && x <= max;
}
// ...
if (between(x, 0.001, 0.009)) {
// something
}
回答by Alexander
Here is an option with only a single comparison.
这是一个只有一个比较的选项。
// return true if in range, otherwise false
function inRange(x, min, max) {
return ((x-min)*(x-max) <= 0);
}
console.log(inRange(5, 1, 10)); // true
console.log(inRange(-5, 1, 10)); // false
console.log(inRange(20, 1, 10)); // false
回答by Alnitak
If you mustuse a regexp (and really, you shouldn't!) this will work:
如果您必须使用正则表达式(实际上,您不应该!)这将起作用:
/^0\.00([1-8]\d*|90*)$/
should work, i.e.
应该工作,即
^
nothing before,- followed by
0.00
(nb: backslash escape for the.
character) - followed by 1 through 8, and any number of additional digits
- or 9, followed by any number of zeroes
$
: followed by nothing else
^
以前什么都没有- 后跟
0.00
(注意:字符的反斜杠转义.
) - 后跟 1 到 8,以及任意数量的附加数字
- 或 9,后跟任意数量的零
$
: 没有别的
回答by Haroldo_OK
If you're already using lodash
, you could use the inRange()
function:
https://lodash.com/docs/4.17.15#inRange
如果您已经在使用lodash
,则可以使用该inRange()
功能:https:
//lodash.com/docs/4.17.15#inRange
_.inRange(3, 2, 4);
// => true
_.inRange(4, 8);
// => true
_.inRange(4, 2);
// => false
_.inRange(2, 2);
// => false
_.inRange(1.2, 2);
// => true
_.inRange(5.2, 4);
// => false
_.inRange(-3, -2, -6);
// => true
回答by Devil's Advocate
I like Pointy's between
function so I wrote a similar one that worked well for my scenario.
我喜欢 Pointy 的between
功能,所以我写了一个类似的功能,适用于我的场景。
/**
* Checks if an integer is within ±x another integer.
* @param {int} op - The integer in question
* @param {int} target - The integer to compare to
* @param {int} range - the range ±
*/
function nearInt(op, target, range) {
return op < target + range && op > target - range;
}
so if you wanted to see if x
was within ±10 of y
:
所以如果你想看看是否x
在±10以内y
:
var x = 100;
var y = 115;
nearInt(x,y,10) = false
I'm using it for detecting a long-press on mobile:
我用它来检测手机上的长按:
//make sure they haven't moved too much during long press.
if (!nearInt(Last.x,Start.x,5) || !nearInt(Last.y, Start.y,5)) clearTimeout(t);
回答by Favor George
If you want your code to pick a specific range of digits, be sure to use the &&
operator instead of the ||
.
如果您希望代码选择特定范围的数字,请务必使用&&
运算符而不是||
.
if (x >= 4 && x <= 9) {
// do something
} else {
// do something else
}
// be sure not to do this
if (x >= 4 || x <= 9) {
// do something
} else {
// do something else
}
回答by Steve KACOU
You must want to determine the lower and upper bound before writing the condition
在编写条件之前,您必须要确定下限和上限
function between(value,first,last) {
let lower = Math.min(first,last) , upper = Math.max(first,last);
return value >= lower && value <= upper ;
}