检查值是否在数字范围内

检查值是否在数字范围内

问题描述:

我想检查某个值是否在可接受的范围内。如果是,做某事;否则,别的。

I want to check if a value is in an accepted range. If yes, to do something; otherwise, something else.

范围是 0.001-0.009 。我知道如何使用多个 if 来检查这个,但我想知道是否有办法在单个中检查它是否声明。

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.

你问一个关于数字比较的问题,所以正则表达式实际上与问题。您不需要多个 if 语句来执行此操作:

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
}

你可以自己写一个between()函数:

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
}