Forces a numerical value to be between a min and a max. - Java java.lang

Java examples for java.lang:Math Value

Description

Forces a numerical value to be between a min and a max.

Demo Code


//package com.java2s;

public class Main {
    /**/*  w  w  w .j a va  2s.  co  m*/
     * Forces a numerical value to be between a min
     * and a max.
     *
     * @param min   If less than min, returns min
     * @param max   If greater than max, returns max
     * @param value Value to test
     * @return Coerced value
     */
    public static double coerce(double min, double max, double value) {
        return (value > max) ? max : (value < min) ? min : value;
    }
}

Related Tutorials