Check if a value is within a range of numbers
NickName:Sotiris Ask DateTime:2011-06-23T20:44:55

Check if a value is within a range of numbers

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.

Copyright Notice:Content Author:「Sotiris」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/6454198/check-if-a-value-is-within-a-range-of-numbers

Answers
Favour George 2018-05-13T20:00:18

If you want your code to pick a specific range of digits, be sure to use the && operator instead of the ||. \n\n\r\n\r\nif (x >= 4 && x <= 9) {\r\n // do something\r\n} else {\r\n // do something else\r\n}\r\n\r\n// be sure not to do this\r\n\r\nif (x >= 4 || x <= 9) {\r\n // do something\r\n} else {\r\n // do something else\r\n}",


De Bonheur 2019-09-25T13:58:39

You must want to determine the lower and upper bound before writing the condition\n\nfunction between(value,first,last) {\n\n let lower = Math.min(first,last) , upper = Math.max(first,last);\n return value >= lower && value <= upper ;\n\n}\n",


Pointy 2011-06-23T12:47:28

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:\n\nif (x >= 0.001 && x <= 0.009) {\n // something\n}\n\n\nYou could write yourself a \"between()\" function:\n\nfunction between(x, min, max) {\n return x >= min && x <= max;\n}\n// ...\nif (between(x, 0.001, 0.009)) {\n // something\n}\n",


Alexander 2018-04-09T02:51:49

Here is an option with only a single comparison.\n\n// return true if in range, otherwise false\nfunction inRange(x, min, max) {\n return ((x-min)*(x-max) <= 0);\n}\n\nconsole.log(inRange(5, 1, 10)); // true\nconsole.log(inRange(-5, 1, 10)); // false\nconsole.log(inRange(20, 1, 10)); // false\n",


Alnitak 2011-06-23T12:55:28

If you must use a regexp (and really, you shouldn't!) this will work:\n\n/^0\\.00([1-8]\\d*|90*)$/\n\n\nshould work, i.e.\n\n\n^ nothing before,\nfollowed by 0.00 (nb: backslash escape for the . character)\nfollowed by 1 through 8, and any number of additional digits\nor 9, followed by any number of zeroes\n$: followed by nothing else\n",


Haroldo_OK 2020-02-17T19:44:36

If you're already using lodash, you could use the inRange() function:\nhttps://lodash.com/docs/4.17.15#inRange\n\n_.inRange(3, 2, 4);\n// => true\n\n_.inRange(4, 8);\n// => true\n\n_.inRange(4, 2);\n// => false\n\n_.inRange(2, 2);\n// => false\n\n_.inRange(1.2, 2);\n// => true\n\n_.inRange(5.2, 4);\n// => false\n\n_.inRange(-3, -2, -6);\n// => true\n",


user736893 2017-06-07T15:14:42

I like Pointy's between function so I wrote a similar one that worked well for my scenario.\n\n/**\n * Checks if an integer is within ±x another integer.\n * @param {int} op - The integer in question\n * @param {int} target - The integer to compare to\n * @param {int} range - the range ±\n */\nfunction nearInt(op, target, range) {\n return op < target + range && op > target - range;\n}\n\n\nso if you wanted to see if x was within ±10 of y:\n\nvar x = 100;\nvar y = 115;\nnearInt(x,y,10) = false\n\n\nI'm using it for detecting a long-press on mobile:\n\n//make sure they haven't moved too much during long press.\nif (!nearInt(Last.x,Start.x,5) || !nearInt(Last.y, Start.y,5)) clearTimeout(t);\n",


More about “Check if a value is within a range of numbers” related questions

Check if there is no consecutive numbers in range

I've got a range of values. I need to check for each value above zero: If any two values in range are consecutive (located next to each other, like 7 an 8) - type "Yes". Otherwise - "No". Update...

Show Detail

Check if a range of numbers are within another range in PHP, or MySQL

It would have been ideal to do what I want to do in a MySQL query but I think I maybe quite limited after looking at BETWEEN. So I've been looking at PHP... So I have 2 numbers for a property siz...

Show Detail

How to check if an integer is within a range of numbers in PHP?

How can I check if a given number is within a range of numbers?

Show Detail

Check if a value is within a range of numbers

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...

Show Detail

JavaScript check if several numbers within a set are within a certain range

Basically, I have a list like this in JavaScript. var times1=[0,100,500,501,502,503,504] var times2=[0,50,100,150,200,250,300] And I need to check if any 5 numbers have a range of 10. For example,

Show Detail

Check value range from two numbers in javascript

I have two fields and a check made in javascript &lt;input type="text" id="numbers" /&gt; &lt;input type="text" id="via" /&gt; if (((document.getElementById("via").

Show Detail

Checking whether a list value is within a range

I have created a list of numbers in fav_numbers. The user will be prompted to enter 2 values: starting range and ending range. The program will check if the list of numbers will fall into the range...

Show Detail

XORing of numbers within the given range

Here in this program of XORing of numbers within the given range, I can't understand the algorithm of how they are finding xor of numbers. It would be pretty good if someone explains how this algor...

Show Detail

Is there a function in Scheme/Racket that can be used to check if a number lies within a range of numbers

What is the function in Scheme/Racket that can be used to check if a real number lies within a given range of numbers.

Show Detail

Check value within range in Stored Procedure

I have some specific count value and I have to check if that count lies within what range. The ranges are specified in the below table. For example,if the count value is &gt;= 1 AND less than 25000...

Show Detail