Java Number Round round(int a, int cutOfDigits)

Here you can find the source of round(int a, int cutOfDigits)

Description

Math#round(double) -like operation for integers, e.g.:
  • 1234, 3 --> 1000 (like floor, since digits are < 5)
  • 1234, 2 --> 1200 (like floor, since digits are < 5)
  • 1234, 1 --> 1230 (like floor, since digits are < 5)
  • 5678, 3 --> 6000 (like ceil, since digits are >= 5)
  • 5678, 2 --> 5700 (like ceil, since digits are >= 5)
  • 5678, 1 --> 5680 (like ceil, since digits are >= 5)
Some "unusual" cases:
  • 1234, 5 --> 0 (like floor, since digits are < 5)
  • 1234, 4 --> 0 (like floor, since digits are < 5)
  • 1234, 0 --> 1234 (like floor, since digits are < 5)
  • 1234, -1 --> 1234 (like floor, since digits are < 5)
  • 5678, 5 --> 100000 (like ceil, since digits are >= 5)
  • 5678, 4 --> 10000 (like ceil, since digits are >= 5)
  • 5678, 0 --> 5678 (like ceil, since digits are >= 5)
  • 5678, -1 --> 5678 (like ceil, since digits are >= 5)

License

Open Source License

Parameter

Parameter Description
a - a value
cutOfDigits - the number of digits to "cut of"

Return

the new value

Declaration

public static int round(int a, int cutOfDigits) 

Method Source Code

//package com.java2s;
/**//from  w ww .  ja  va 2  s . c  om
 * Syncnapsis Framework - Copyright (c) 2012-2014 ultimate
 * 
 * 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 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 MECHANTABILITY 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 Plublic License along with this program;
 * if not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Modification of {@link Math#round(double)} taking an additional argument representing the
     * requested accuracy in the following way:<br>
     * <code>double fac = Math.pow(10, digits);<br>
     * return fac * Math.round(a / fac);</code>
     * 
     * @param a - a value
     * @param cutOfDigits - the number of digits to shift the value before and after rounding
     * @return the new value
     */
    public static double round(double a, int cutOfDigits) {
        double fac = Math.pow(10, cutOfDigits);
        return fac * Math.round(a / fac);
    }

    /**
     * {@link Math#round(double)}-like operation for integers, e.g.:
     * <ul>
     * <li>1234, 3 --> 1000 (like floor, since digits are < 5)</li>
     * <li>1234, 2 --> 1200 (like floor, since digits are < 5)</li>
     * <li>1234, 1 --> 1230 (like floor, since digits are < 5)</li>
     * <li>5678, 3 --> 6000 (like ceil, since digits are >= 5)</li>
     * <li>5678, 2 --> 5700 (like ceil, since digits are >= 5)</li>
     * <li>5678, 1 --> 5680 (like ceil, since digits are >= 5)</li>
     * </ul>
     * Some "unusual" cases:
     * <ul>
     * <li>1234, 5 --> 0 (like floor, since digits are < 5)</li>
     * <li>1234, 4 --> 0 (like floor, since digits are < 5)</li>
     * <li>1234, 0 --> 1234 (like floor, since digits are < 5)</li>
     * <li>1234, -1 --> 1234 (like floor, since digits are < 5)</li>
     * <li>5678, 5 --> 100000 (like ceil, since digits are >= 5)</li>
     * <li>5678, 4 --> 10000 (like ceil, since digits are >= 5)</li>
     * <li>5678, 0 --> 5678 (like ceil, since digits are >= 5)</li>
     * <li>5678, -1 --> 5678 (like ceil, since digits are >= 5)</li>
     * </ul>
     * 
     * @param a - a value
     * @param cutOfDigits - the number of digits to "cut of"
     * @return the new value
     */
    public static int round(int a, int cutOfDigits) {
        if (cutOfDigits <= 0)
            return a;
        int fac = 1;
        while (--cutOfDigits >= 0)
            fac *= 10;
        int ret = (a / fac) * fac;
        if (a - ret > fac / 2)
            ret += fac;
        return ret;
    }
}

Related

  1. round(final long timeMs, final int precision)
  2. round(int f)
  3. round(int n)
  4. round(int start, int boundary)
  5. round(int t, int n, int decimals)