Java Float Number Clamp clamp(float f, float min, float max)

Here you can find the source of clamp(float f, float min, float max)

Description

Rounds the given value so that it's bigger or equal to min and smaller or equal to max.

License

Open Source License

Parameter

Parameter Description
min a parameter
max a parameter

Declaration

public static float clamp(float f, float min, float max) 

Method Source Code

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

public class Main {
    /**// ww  w .j a  v a  2 s  .  c o m
     * Rounds the given value so that it's bigger or 
     * equal to min and smaller or equal to max.
     * @param float
     * @param min
     * @param max
     * @returns the clamped value as a float
     */
    public static float clamp(float f, float min, float max) {

        float r = f;

        if (r < min)
            r = min;
        if (r > max)
            r = max;

        return r;
    }

    /**
     * Rounds the given value so that it's bigger or 
     * equal to min and smaller or equal to max.
     * @param int
     * @param min
     * @param max
     * @returns the clamped value as an int
     */
    public static int clamp(int i, int min, int max) {

        int r = i;

        if (r < min)
            r = min;
        if (r > max)
            r = max;

        return r;
    }
}

Related

  1. clamp(final float value, final float min, final float max)
  2. clamp(final float x, final float a, final float b)
  3. Clamp(float a, float min, float max)
  4. clamp(float a, float min, float max)
  5. clamp(float f)
  6. clamp(float f, float min, float max)
  7. clamp(float input, float min, float max)
  8. clamp(float min, float max, float val)
  9. clamp(float min, float max, float value)