Java Integer Clamp clamp(int n, int min, int max)

Here you can find the source of clamp(int n, int min, int max)

Description

Restrains a number in a interval

License

Open Source License

Parameter

Parameter Description
n the original number
min the minimum value of the interval
max the maximum value of the interval

Return

a valid number inside the said interval. If min > max, then the interval will be reversed

Declaration

public static int clamp(int n, int min, int max) 

Method Source Code

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

public class Main {
    /**/*from  w w  w .ja  v  a 2s  .c o m*/
     * Restrains a number in a interval
     * @param n the original number
     * @param min the minimum value of the interval
     * @param max the maximum value of the interval
     * @return a valid number inside the said interval. If min > max, then the interval will be reversed
     */
    public static int clamp(int n, int min, int max) {

        if (min > max)
            return clamp(n, max, min);

        if (n < min)
            n = min;

        if (n > max)
            n = max;

        return n;
    }

    /**
     * Restrains a number in a interval
     * @param n the original number
     * @param min the minimum value of the interval
     * @param max the maximum value of the interval
     * @return a valid number inside the said interval. If min > max, then the interval will be reversed
     */
    public static float clamp(float n, float min, float max) {

        if (min > max)
            return clamp(n, max, min);

        if (n < min)
            n = min;

        if (n > max)
            n = max;

        return n;
    }
}

Related

  1. clamp(int min, int val, int max)
  2. clamp(int min, int value, int max)
  3. clamp(int n, int l, int u)
  4. clamp(int n, int lower, int upper)
  5. clamp(int n, int min, int max)
  6. clamp(int num, int min, int max)
  7. clamp(int number, int low, int high)
  8. clamp(int ptr, int size)
  9. clamp(int v, int min, int max)