Java Double Number Clamp clamp(double val, double min, double max)

Here you can find the source of clamp(double val, double min, double max)

Description

Clamps the given value using the specified maximum and minimum.

License

Open Source License

Parameter

Parameter Description
val the value to be clamped.
min the minimum to which the given value should be clamped.
max the maximum to which the given value should be clamped

Return

the clamped value, i.e. val if val is between min and max , max if val is greater than max or min if val is smaller than min .

Declaration

public static double clamp(double val, double min, double max) 

Method Source Code

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

public class Main {
    /**/* w  ww.j  ava  2s.  c om*/
     * Clamps the given value using the specified maximum and minimum.
     *
     * @param val the value to be clamped.
     * @param min the minimum to which the given value should be clamped.
     * @param max the maximum to which the given value should be clamped
     * @return the clamped value, i.e. {@code val} if {@code val} is between {@code min} and {@code max}, {@code max }
     * if {@code val} is greater than {@code max} or {@code min} if {@code val} is smaller than {@code min}.
     */
    public static double clamp(double val, double min, double max) {
        return val < min ? min : val > max ? max : val;
    }

    /**
     * Clamps the given value using the specified maximum and minimum.
     *
     * @param val the value to be clamped.
     * @param min the minimum to which the given value should be clamped.
     * @param max the maximum to which the given value should be clamped
     * @return the clamped value, i.e. {@code val} if {@code val} is between {@code min} and {@code max}, {@code max }
     * if {@code val} is greater than {@code max} or {@code min} if {@code val} is smaller than {@code min}.
     */
    public static int clamp(int val, int min, int max) {
        return val < min ? min : val > max ? max : val;
    }
}

Related

  1. clamp(double v, double lower, double upper)
  2. clamp(double v, double lower, double upper)
  3. clamp(double val)
  4. clamp(double val)
  5. clamp(double val, double min, double max)
  6. clamp(double value)
  7. clamp(double value)
  8. clamp(double value, double low, double high)
  9. clamp(double value, double low, double high)