Java Double Number Clamp clamp(final double value, final double min, final double max)

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

Description

clamp

License

LGPL

Declaration

public static double clamp(final double value, final double min, final double max) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

public class Main {
    public static double clamp(final double value, final double min, final double max) {
        if (min > max) {
            throw new IllegalArgumentException("min > max");
        }//from   ww  w .  j a v  a2  s  . com

        if (value < min) {
            return min;
        } else if (value > max) {
            return max;
        } else {
            return value;
        }
    }

    public static float clamp(final float value, final float min, final float max) {
        if (min > max) {
            throw new IllegalArgumentException("min > max");
        }

        if (value < min) {
            return min;
        } else if (value > max) {
            return max;
        } else {
            return value;
        }
    }

    public static int clamp(final int value, final int min, final int max) {
        if (min > max) {
            throw new IllegalArgumentException("min > max");
        }

        if (value < min) {
            return min;
        } else if (value > max) {
            return max;
        } else {
            return value;
        }
    }

    public static long clamp(final long value, final long min, final long max) {
        if (min > max) {
            throw new IllegalArgumentException("min > max");
        }

        if (value < min) {
            return min;
        } else if (value > max) {
            return max;
        } else {
            return value;
        }
    }
}

Related

  1. clamp(double weight, double lowerBound, double upperBound)
  2. clamp(double x, double low, double high)
  3. clamp(double x, double min, double max)
  4. clamp(double x, double min, double max)
  5. clamp(double x, double xMin, double xMax)
  6. clamp(final double value, final double min, final double max)
  7. clamp01(double d)
  8. clamp01(double value)
  9. clamp255d(double d)