Java Float Number Round round(final float x, final float roundFactor)

Here you can find the source of round(final float x, final float roundFactor)

Description

Computes a rounded value for the given rounding factor.

License

Open Source License

Parameter

Parameter Description
x the value to be rounded
roundFactor the rounding factor specifying the accuracy, should always be a power to the base 10

Return

the rounded value

Declaration

public static float round(final float x, final float roundFactor) 

Method Source Code

//package com.java2s;
/*// w  w  w.  j  a  v a  2 s. c  o 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 {
    /**
     * 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;
    }
}

Related

  1. round(final float a)
  2. round(final float f)
  3. round(final float val)
  4. round(final float value)
  5. round(float a)
  6. round(float amount, int cent)
  7. round(float axisValue)
  8. round(float d)