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

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

Description

Clamp Integer values to a given range

License

Open Source License

Parameter

Parameter Description
value the value to clamp
min the minimum value
max the maximum value

Return

the clamped value

Declaration

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

Method Source Code

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

public class Main {
    /**// ww  w . j av a 2s.  c  o m
     * Clamp Integer values to a given range
     *
     * @param value     the value to clamp
     * @param min       the minimum value
     * @param max       the maximum value
     * @return          the clamped value
     */
    public static int clamp(int value, int min, int max) {
        return Math.max(min, Math.min(max, value));
    }

    /**
     * Clamp Float values to a given range
     *
     * @param value     the value to clamp
     * @param min       the minimum value
     * @param max       the maximum value
     * @return          the clamped value
     */
    public static float clamp(float value, float min, float max) {
        return Math.max(min, Math.min(max, value));
    }

    /**
     * Clamp Long values to a given range
     *
     * @param value     the value to clamp
     * @param min       the minimum value
     * @param max       the maximum value
     * @return          the clamped value
     */
    public static long clamp(long value, long min, long max) {
        return Math.max(min, Math.min(max, value));
    }

    /**
     * Clamp Double values to a given range
     *
     * @param value     the value to clamp
     * @param min       the minimum value
     * @param max       the maximum value
     * @return          the clamped value
     */
    public static double clamp(double value, double min, double max) {
        return Math.max(min, Math.min(max, value));
    }
}

Related

  1. clamp(int value, int min, int max)
  2. clamp(int value, int min, int max)
  3. clamp(int value, int min, int max)
  4. clamp(int value, int min, int max)
  5. clamp(int value, int min, int max)
  6. clamp(int value, int min, int max)
  7. clamp(int value, int minValue, int maxValue)
  8. clamp(int var, int min, int max)
  9. clamp(int x, int min, int max)