Restricts a double x to an interval [min, max]. - Java java.lang

Java examples for java.lang:double

Description

Restricts a double x to an interval [min, max].

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        double xx = 2.45678;
        double min = 2.45678;
        double max = 2.45678;
        System.out.println(bound(xx, min, max));
    }/*from w  w  w.j  a  v a  2 s  .  c  om*/

    /**
     * Restricts a double x to an interval [min, max].
     * @param x The input parameter
     * @param min The minimum accepted value of 'x'. 
     * @param max The maximum accepted value of 'x'. 
     * @return
     */
    public static final double bound(double xx, double min, double max) {
        double x = xx;
        if (x < min)
            return min;
        if (x > max)
            return max;
        return x;
    }
}

Related Tutorials