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

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

Description

Clamps an int value to a given range.

License

Open Source License

Parameter

Parameter Description
x the value to clamp
min the minimum value of the range
max the maximum value of the range

Return

the clamped value

Declaration

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

Method Source Code

//package com.java2s;
/*/*from w  w w .j  a  va  2 s  . c om*/
 * Copyright (c) 2016 Martin Davis.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 *
 * http://www.eclipse.org/org/documents/edl-v10.php.
 */

public class Main {
    /**
     * Clamps a <tt>double</tt> value to a given range.
     * @param x the value to clamp
     * @param min the minimum value of the range
     * @param max the maximum value of the range
     * @return the clamped value
     */
    public static double clamp(double x, double min, double max) {
        if (x < min)
            return min;
        if (x > max)
            return max;
        return x;
    }

    /**
     * Clamps an <tt>int</tt> value to a given range.
     * @param x the value to clamp
     * @param min the minimum value of the range
     * @param max the maximum value of the range
     * @return the clamped value
     */
    public static int clamp(int x, int min, int max) {
        if (x < min)
            return min;
        if (x > max)
            return max;
        return x;
    }
}

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 minValue, int maxValue)
  5. clamp(int var, int min, int max)
  6. clamp(int x, int min, int max)
  7. clamp(String string, int maxChars)
  8. clamp_int(int num, int min, int max)
  9. clamp_wrap(int a, int x, int y)