Java Integer Clamp clamp(int i, int low, int high)

Here you can find the source of clamp(int i, int low, int high)

Description

Converts input number to a number within the specified range.

License

Open Source License

Parameter

Parameter Description
i input number
low minimum range
high maximum range

Return

clamped number

Declaration

public static int clamp(int i, int low, int high) 

Method Source Code

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

public class Main {
    /**//from w w w.j  a v a2s .  c  o m
     * Converts input number to a number within the specified range.
     * @param i input number
     * @param low minimum range
     * @param high maximum range
     * @return clamped number
     */
    public static int clamp(int i, int low, int high) {
        return Math.max(Math.min(i, high), low);
    }

    /**
     * Converts input number to a number within the specified range.
     * @param i input number
     * @param low minimum range
     * @param high maximum range
     * @return clamped number
     */
    public static long clamp(long i, long low, long high) {
        return Math.max(Math.min(i, high), low);
    }

    /**
     * Converts input number to a number within the specified range.
     * @param i input number
     * @param low minimum range
     * @param high maximum range
     * @return clamped number
     */
    public static double clamp(double i, double low, double high) {
        return Math.max(Math.min(i, high), low);
    }

    /**
     * Converts input number to a number within the specified range.
     * @param i input number
     * @param low minimum range
     * @param high maximum range
     * @return clamped number
     */
    public static float clamp(float i, float low, float high) {
        return Math.max(Math.min(i, high), low);
    }
}

Related

  1. clamp(int a, int x, int y)
  2. clamp(int amt, int low, int high)
  3. clamp(int c)
  4. clamp(int c)
  5. clamp(int floor, int ceiling, int value)
  6. clamp(int i, int min, int max)
  7. clamp(int min, int val, int max)
  8. clamp(int min, int value, int max)
  9. clamp(int n, int l, int u)