Here you can find the source of roundAndCrop(final float x, final int min, final int max)
Math.round
with x
and then crops the resulting value to the range min
to max
.
public static int roundAndCrop(final float x, final int min, final int max)
//package com.java2s; /*/*w ww . j a v a 2 s .c om*/ * 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.round</code> with <code>x</code> and then crops the resulting value to the range * <code>min</code> to <code>max</code>. */ public static int roundAndCrop(final float x, final int min, final int max) { final int rx = Math.round(x); return crop(rx, min, max); // return (rx < min) ? min : (rx > max) ? max : rx; } /** * First calls <code>Math.round</code> with <code>value</code> and then crops the resulting value to the range * <code>min</code> to <code>max</code>. * @param value the value to round and crop * @param min the minimum value of the crop range * @param max the maximum value of the crop range * * @return the rounded and cropped value */ public static long roundAndCrop(final double value, final long min, final long max) { final long rx = Math.round(value); return crop(rx, min, max); } /** * Computes a rounded value for the given rounding factor. The given value is pre-multiplied with the rounding * factor, then rounded to the closest <code>int</code> and then again divided by the the rounding factor. * <p>The rounding factor can be computed for a given value range and accuracy with the * <code>computeRoundFactor</code> method. * * @param x the value to be rounded * @param roundFactor the rounding factor specifying the accuracy, should always be a power to the base 10 * * @return the rounded value * * @see #computeRoundFactor(float, float, int) */ public static float round(final float x, final float roundFactor) { return (float) Math.round(x * roundFactor) / roundFactor; } /** * Computes a rounded value for the given rounding factor. The given value is pre-multiplied with the rounding * factor, then rounded to the closest <code>int</code> and then again divided by the the rounding factor. * <p>The rounding factor can be computed for a given value range and accuracy with the * <code>computeRoundFactor</code> method. * * @param x the value to be rounded * @param roundFactor the rounding factor specifying the accuracy, should always be a power to the base 10 * * @return the rounded value * * @see #computeRoundFactor(double, double, int) */ public static double round(final double x, final double roundFactor) { return (double) Math.round(x * roundFactor) / roundFactor; } /** * 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; } }