Java floor floorAndCrop(final double x, final int min, final int max)

Here you can find the source of floorAndCrop(final double x, final int min, final int max)

Description

First calls Math.floor with x and then crops the resulting value to the range min to max.

License

Open Source License

Declaration

public static int floorAndCrop(final double x, final int min, final int max) 

Method Source Code

//package com.java2s;
/*// www  .j  a  v  a2  s.co m
 * Copyright (C) 2010 Brockmann Consult GmbH (info@brockmann-consult.de)
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 3 of the License, or (at your option)
 * any later version.
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, see http://www.gnu.org/licenses/
 */

public class Main {
    /**
     * First calls <code>Math.floor</code> with <code>x</code> and then crops the resulting value to the range
     * <code>min</code> to <code>max</code>.
     */
    public static int floorAndCrop(final double x, final int min, final int max) {
        final int rx = floorInt(x);
        return crop(rx, min, max);
        //        return (rx < min) ? min : (rx > max) ? max : rx;
    }

    /**
     * Returns <code>(int) Math.floor(value)</code>.
     *
     * @param value the <code>double</code> value to be converted
     *
     * @return the integer value corresponding to the floor of <code>value</code>
     */
    public static int floorInt(final double value) {
        return (int) Math.floor(value);
    }

    /**
     * Crops the value to the range <code>min</code> to <code>max</code>.
     *
     * @param val the value to crop
     * @param min the minimum crop limit
     * @param max the maximum crop limit
     */
    public static byte crop(byte val, byte min, byte max) {
        return val < min ? min : val > max ? max : val;
    }

    /**
     * Crops the value to the range <code>min</code> to <code>max</code>.
     *
     * @param val the value to crop
     * @param min the minimum crop limit
     * @param max the maximum crop limit
     */
    public static short crop(short val, short min, short max) {
        return val < min ? min : val > max ? max : val;
    }

    /**
     * Crops the value to the range <code>min</code> to <code>max</code>.
     *
     * @param val the value to crop
     * @param min the minimum crop limit
     * @param max the maximum crop limit
     */
    public static int crop(int val, int min, int max) {
        return val < min ? min : val > max ? max : val;
    }

    /**
     * Crops the value to the range <code>min</code> to <code>max</code>.
     *
     * @param val the value to crop
     * @param min the minimum crop limit
     * @param max the maximum crop limit
     */
    public static long crop(long val, long min, long max) {
        return val < min ? min : val > max ? max : val;
    }

    /**
     * Crops the value to the range <code>min</code> to <code>max</code>.
     *
     * @param val the value to crop
     * @param min the minimum crop limit
     * @param max the maximum crop limit
     */
    public static float crop(float val, float min, float max) {
        return val < min ? min : val > max ? max : val;
    }

    /**
     * Crops the value to the range <code>min</code> to <code>max</code>.
     *
     * @param val the value to crop
     * @param min the minimum crop limit
     * @param max the maximum crop limit
     */
    public static double crop(double val, double min, double max) {
        return val < min ? min : val > max ? max : val;
    }
}

Related

  1. floor2(int a, int preserveDigits)
  2. floor_double(double a)
  3. floor_double(double value)
  4. floor_double_long(double d)
  5. floor_float(float f)
  6. floorData(float[] data, float floor)
  7. floorDay(long milli)
  8. floorDiv(final int x, final int y)
  9. floorDiv(int dividend, int divisor)