检查值是否在数字范围内 [英] Check if a value is within a range of numbers

查看:122
本文介绍了检查值是否在数字范围内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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
}

这篇关于检查值是否在数字范围内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆