if value is between two numbers

前端 未结 5 2062
无人共我
无人共我 2020-12-18 23:39

I want to be able to test whether a value is within a number range. This is my jQuery code...

if ((year < 2099) && (year > 1990)){
    return \         


        
相关标签:
5条回答
  • 2020-12-18 23:57

    You could make your own method:

    // jquery
    $(function() {
        var myNumber = 100;
        try {
            if (myNumber.isBetween(50, 150)) 
                alert(myNumber + " is between 50 and 100.");
            else 
                alert(myNumber + " is not between 50 and 100.");
        } catch (e) {
            alert(e.message());
        }
    
    });
    
    // js prototype
    if (typeof(Number.prototype.isBetween) === "undefined") {
        Number.prototype.isBetween = function(min, max, notBoundaries) {
            var between = false;
            if (notBoundaries) {
                if ((this < max) && (this > min)) between = true;
                alert('notBoundaries');
            } else {
                if ((this <= max) && (this >= min)) between = true;
                alert('Boundaries');
            }
            alert('here');
            return between;
        }
    }
    

    hope this helps.

    Max

    0 讨论(0)
  • 2020-12-19 00:04

    In many languages, the second way will be evaluated from left to right incorrectly with regard to what you want.

    In C, for instance, 1990 < year will evaluate to 0 or 1, which then becomes 1 < 2099, which is always true, of course.

    Javascript is a quite similar to C: 1990 < year returns true or false, and those boolean expressions seem to numerically compare equal to 0 and 1 respectively.

    But in C#, it won't even compile, giving you the error:

    error CS0019: Operator '<' cannot be applied to operands of type 'bool' and 'int'

    You get a similar error from Ruby, while Haskell tells you that you cannot use < twice in the same infix expression.

    Off the top of my head, Python is the only language that I'm sure handles the "between" setup that way:

    >>> year = 5
    >>> 1990 < year < 2099
    False
    >>> year = 2000
    >>> 1990 < year < 2099
    True
    

    The bottom line is that the first way (x < y && y < z) is always your safest bet.

    0 讨论(0)
  • 2020-12-19 00:13

    If you don't like the boolean operator, you could always use nested if statements:

    if (1990 < year)
    {
        if( year < 2099)
            return 'good stuff';
    }
    
    0 讨论(0)
  • 2020-12-19 00:15

    From a similar solution here: http://indisnip.wordpress.com/2010/08/26/quicktip-check-if-a-number-is-between-two-numbers/

    $.fn.between = function(a,b){
        return (a < b ? this[0] >= a && this[0] <= b : this[0] >= b && this[0] <= a);
    }
    
    0 讨论(0)
  • 2020-12-19 00:21

    The fast and simple way to make this is to create a function like this:

    function inRange(n, nStart, nEnd)
    {
        if(n>=nStart && n<=nEnd) return true;
        else return false;
    }
    

    Then use that as follows:

    inRange(500, 200, 1000) => this return true;
    

    Or like this:

    inRange(199, 200, 1000) => this return false;
    
    0 讨论(0)
提交回复
热议问题