Java Number Range Check inRange(int n, int lo, int hi)

Here you can find the source of inRange(int n, int lo, int hi)

Description

Avoid a number getting out of a certain range

License

Open Source License

Parameter

Parameter Description
n The number to be checked
lo The Lower margin of the number. n cannot be smaller than this
hi The Upper margin of the number. n cannot be bigger than this

Return

If n is out of range, set to defined extreme values, otherwise return n

Declaration

public static int inRange(int n, int lo, int hi) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//  w w  w  .  ja v a  2 s .  c  o  m
     * Avoid a number getting out of a certain range
     * @param n The number to be checked
     * @param lo The Lower margin of the number. n cannot be smaller than this
     * @param hi The Upper margin of the number. n cannot be bigger than this
     * @return If n is out of range, set to defined extreme values, otherwise return n
     */
    public static int inRange(int n, int lo, int hi) {
        return n < lo ? lo : (n > hi ? hi : n);
    }

    /**
     * Avoid a number getting out of a certain range
     * @param n The number to be checked
     * @param lo The Lower margin of the number. n cannot be smaller than this
     * @param hi The Upper margin of the number. n cannot be bigger than this
     * @return If n is out of range, set to defined extreme values, otherwise return n
     */
    public static double inRange(double n, double lo, double hi) {
        return n < lo ? lo : (n > hi ? hi : n);
    }
}

Related

  1. inRange(int check, int range)
  2. inRange(int checkValue, int min, int max)
  3. inRange(int firstIndex, int secondIndex)
  4. inRange(int l, int u, int v, int... v_)
  5. inRange(int minValue, int maxValue, int value)
  6. inRange(int num, int from, int to, int flag)
  7. inRange(int value, int lowerBound, int upperBound)
  8. inRange(int value, int min, int max)
  9. inRange(int value, int min, int max)