Java Ceil ceil2(int a, int preserveDigits)

Here you can find the source of ceil2(int a, int preserveDigits)

Description

Math#ceil(double) -like operation for integers preserving the given number of digits in contrary to MathUtil#ceil(int,int) where cut-of-digits are specified:
return ceil(a, getDigits(a) - preserveDigits)

License

Open Source License

Parameter

Parameter Description
a - a value
preserveDigits - the number of digits to preserve

Return

the new value

Declaration

public static int ceil2(int a, int preserveDigits) 

Method Source Code

//package com.java2s;
/**//from  w ww  . j ava 2s . c  o m
 * 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 {
    /**
     * {@link Math#ceil(double)}-like operation for integers preserving the given number of digits
     * in contrary to {@link MathUtil#ceil(int, int)} where cut-of-digits are specified:<br>
     * <code>return ceil(a, getDigits(a) - preserveDigits)</code>
     * 
     * @param a - a value
     * @param preserveDigits - the number of digits to preserve
     * @return the new value
     */
    public static int ceil2(int a, int preserveDigits) {
        return ceil(a, getDigits(a) - preserveDigits);
    }

    /**
     * Modification of {@link Math#ceil(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.ceil(a / fac);</code>
     * 
     * @param a - a value
     * @param cutOfDigits - the number of digits to shift the value before and after ceiling
     * @return the new value
     */
    public static double ceil(double a, int cutOfDigits) {
        double fac = Math.pow(10, cutOfDigits);
        return fac * Math.ceil(a / fac);
    }

    /**
     * {@link Math#ceil(double)}-like operation for integers, e.g.:
     * <ul>
     * <li>1234, 3 --> 2000</li>
     * <li>1234, 2 --> 1300</li>
     * <li>1234, 1 --> 1240</li>
     * </ul>
     * Some "unusual" cases:
     * <ul>
     * <li>1234, 5 --> 100000</li>
     * <li>1234, 4 --> 10000</li>
     * <li>1234, 0 --> 1234</li>
     * <li>1234, -1 --> 1234</li>
     * </ul>
     * 
     * @param a - a value
     * @param cutOfDigits - the number of digits to "cut of"
     * @return the new value
     */
    public static int ceil(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)
            ret += fac;
        return ret;
    }

    /**
     * Get the number of digits for the given value
     * 
     * @param a - a value
     * @return the number of digits
     */
    public static int getDigits(int a) {
        int digits = 0;
        while (a != 0) {
            digits++;
            a /= 10;
        }
        return digits;
    }
}

Related

  1. ceil(long a, long b)
  2. Ceil(Object in, int val)
  3. ceil(Short a)
  4. ceil10(double a)
  5. ceil2(double d, int p)
  6. ceilDiv(int numerator, int denominator)
  7. ceilDiv(int v1, int v2)
  8. ceilDiv(long a, long b)
  9. ceilDivide(long a, long b)